apache 通过重定向防止目录列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1229127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Preventing directory listing by redirecting?
提问by MarkL
I want to use .htaccessto prevent directory listing.
I've got pages within /location/but I don't have an index file. So I want to redirect to /location/about.phpfor example.
我想用来.htaccess防止目录列表。
我在/location/ 中有页面,但我没有索引文件。所以我想重定向到/location/about.php例如。
Is there a way to do this, without creating an index.htmland redirecting requests to that?
有没有办法做到这一点,而无需创建index.html并将请求重定向到那个?
Thanks for the help!
谢谢您的帮助!
回答by Joe
If you're asking for a file in place of 'index.html', see "DirectoryIndex" to tell it what files to use in place of 'index.html':
如果您要求使用文件代替“index.html”,请参阅“ DirectoryIndex”以告诉它使用哪些文件代替“index.html”:
DirectoryIndex about.php index.html
Options –Indexes
... if you're trying to redirect all directories to a single page, then I'd cheat and do the following, which will mostly do what you're asking for:
...如果您试图将所有目录重定向到一个页面,那么我会作弊并执行以下操作,这主要是为了满足您的要求:
Options +Indexes
IndexOptions +SuppressHTMLPreamble
IndexIgnore *
HeaderName /includes/header.html
ReadmeName /includes/readme.html
... and set /includes/header.html with whatever message you want (or containing a meta-redirect), and /includes/readme.html to be blank.
...并将 /includes/header.html 设置为您想要的任何消息(或包含元重定向),并将 /includes/readme.html 设置为空白。
回答by MarkL
I know this is an older thread but I just had to implement something similar and did it the following way:
我知道这是一个较旧的线程,但我只需要实现类似的东西并按以下方式进行:
Options -Indexes
ErrorDocument 403 /location/about.php
Basically, Options -Indexesprevents directory listing and ErrorDocument 403rewrites the default 403 message.
基本上,Options -Indexes防止目录列表并ErrorDocument 403重写默认的 403 消息。
回答by rob
Here is a direct link to what you need:
这是您需要的直接链接:
http://www.webweaver.nu/html-tips/web-redirection.shtml
http://www.webweaver.nu/html-tips/web-redirection.shtml
If you need something more advanced to handle parameters or need to remap old URLs to new ones, you can use URL rewriting:
如果您需要更高级的东西来处理参数或需要将旧 URL 重新映射到新 URL,您可以使用 URL 重写:
http://corz.org/serv/tricks/htaccess2.php
http://corz.org/serv/tricks/htaccess2.php
Prevent directory listing (or return an empty directory listing):
防止目录列表(或返回空目录列表):
http://www.besthostratings.com/articles/prevent-directory-listing.html
http://www.besthostratings.com/articles/prevent-directory-listing.html
回答by Justin Johnson
Changing DirectoryIndexand Optionsworks, but using mod_alias means that you don't have to change it back in nested directories.
更改DirectoryIndex并Options有效,但使用 mod_alias 意味着您不必在嵌套目录中将其更改回。
Redirect 303 /location/ /location/about.php

