问题重定向403禁止404找不到

时间:2009-02-13 23:57:38

标签: apache .htaccess mod-rewrite http-status-code-404 http-status-code-403

我的.htaccess的相关部分看起来像这样:

Options -Indexes
<FilesMatch include>
    Order allow,deny
    Deny from all
</FilesMatch>
RedirectMatch 404 ^/include(/.*)$

它产生了以下回应:

  • / include 403
  • / include / 404
  • /include/config.inc 403

我可以通过查看我的模式来判断问题可能出现在(/.*)部分,但我尝试的所有内容都给出了相同的结果;而不是一直得到404我得到一个404的404和其他一切的403。 我正在使用的表达式有什么问题?或者,因为我必须为一些目录执行此操作,是否有一种全面的方法可以将所有403响应转换为404?

更新:我发现通过删除FileMatch我得到了更好的结果,所以我的.htaccess现在看起来像这样:

Options -Indexes
RedirectMatch 404 ^/include(/.*)?$ # Added dlamblin's first suggestion

并生成以下回复:

  • / include 404
  • / include / 404
  • /include/config.inc 403

更新:有趣的是,我发现以下产生了不同的输出:

RedirectMatch 404 ^/include(/?|/.*)$
RedirectMatch 404 ^/template(/?|/.*)$

模板模式适用于所有情况但是include仍然为include中的所有文件生成403(例如/include/config.inc)这可能是目录名称的问题而不是.htaccess的问题归档?

更新:访问/include/config.inc时,我的.htaccess中的以下内容与重定向冲突。

<FilesMatch config>
    Order allow,deny
    Deny from all
</FilesMatch>

8 个答案:

答案 0 :(得分:6)

我可以理解为什么你的RedirectMatch没有捕获/ include,你没有使'/'结束可选,但是/include/config.inc部分有点令人费解。

以下是我在Apache 2.2上的工作:

<FilesMatch /include(/?|/.*)>
    Order allow,deny
    Deny from all
</FilesMatch>

RedirectMatch 404 ^/include(/?|/.*)$

这处理这些情况:

/include 404
/include/ 404
/include/config.inc 404

我必须更改FilesMatch部分才能使/ include部分正常工作。

修改

匹配行也可以在没有&lt; FilesMatch&gt;的情况下使用.htaccess中的部分并给出了预期的结果。

答案 1 :(得分:4)

另一种可能性是不打扰匹配整个路径:

RedirectMatch 404 ^/include

如果存在可能以“/ include”开头的公开可见网址路径(例如“/ includeMe”),则会有一个小的添加内容将私有网址与公共网址分开:

RedirectMatch 404 ^/include(/|$)

答案 2 :(得分:2)

使用重写mod:

RewriteEngine on

RewriteCond %{THE_REQUEST} ^.*/\.
RewriteRule ^(.*)$ - [R=404]

以点开头的每个文件或目录都将重定向到404。

/myDir/.svn => 404
/.gitignore => 404
/dir1/dir2_dir3/

或者将所有403,400个错误更改为404个错误,将其放在 /etc/apache2/conf.d/localized-error-pages 的末尾或 .htaccess

# Will raise a 404 error, because the file <fake_file_for_apache_404.php> doesn't exist.
# We change 403 or 400 to 404 !
ErrorDocument 400 /fake_file_for_apache_404.php
ErrorDocument 403 /fake_file_for_apache_404.php
# We need to rewrite 404 error, else we will have "fake_file_for_apache_404.php not found"
ErrorDocument 404 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL <script type=\"text/javascript\">document.write(document.location.pathname);</script> was not found on this server.</p></body></html>"
ErrorDocument 500 "Server in update. Please comme back later."

答案 3 :(得分:1)

你不想要'^ / include(/.*)?$'

这部分是猜测,但如果将RedirectMatch放在块上方会发生什么。这样,在将该请求重定向到404之前,您不会拒绝(禁止)访问请求。

答案 4 :(得分:1)

更好更简单的方法如下:

1 - 插入主conf(即VHOST之外的世界):

<IfModule mod_alias.c>
RedirectMatch 404 ^/myDirectory(?|\/)$
</IfModule>

myDirectory = css,js,images ...

2 - 您可以设置每个目录 - 索引如下:

允许提供您的“DirectoryIndex”Apache指令内容:

<Directory "/myPathtoMyWebDocRoot/">
    Options Indexes
    AllowOverride None
    Order Allow, Deny
    Allow from all
</Directory>

和其他人的拒绝目录索引:

<Directory "/myPathtoMyWebDocRoot/myDirectory/">
    Options -Indexes 
    AllowOverride None
    Order Allow, Deny
    Allow from all
</Directory>

使用myDirectory = *,css,images,js,secret,...根据您的需要。

答案 5 :(得分:0)

即使在子目录中,这个文件/目录的名称也按预期运行,名称以点开头:

<FilesMatch "^\..+">
    Order allow,deny
    Deny from all
    Satisfy All
</FilesMatch>
RedirectMatch 404 \/\..+$

答案 6 :(得分:0)

在这种情况下,您不需要使用Options -Indexes 我停止将目录的内容显示为列表并显示404错误的解决方案很简单。在项目的根目录中创建.htaccess文件,并编写应保护的目录。

目录结构

- your_root_directory
  - .htaccess
  - 404.html
  - index.html 
  - app
    - other files I do not want to show
  - models
    - other files I do not want to show

的.htaccess

RewriteEngine On
RewriteRule   ^app/ - [R=404,L]
RewriteRule   ^models/ - [R=404,L]

ErrorDocument 404 /your_root_directory/404.html .htaccess的第二行禁止访问app目录及其所有子目录中的列表项。

.htaccess的第三行禁止访问models目录及其所有子目录中的列表项。

.htaccess行的第四行设置了我们自己的404错误(如果你不想显示apache默认错误)。

使用htaccess时,请记住清除浏览器中的缓存。

答案 7 :(得分:0)

ErrorDocument 400 /fake_file_for_apache_404.php
ErrorDocument 403 /fake_file_for_apache_404.php
ErrorDocument 404 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL <script type=\"text/javascript\">document.write(document.location.pathname);</script> was not found on this server.</p></body></html>"
ErrorDocument 500 "Server in update. Please comme back later."

我尝试了此解决方案,但遇到了另一个问题:

此外,尝试使用ErrorDocument处理请求时遇到403禁止错误:

Screenshot