这里已经发布了几个关于在特殊情况下返回404而不是403的问题(例如,.ht*文件或某个目录),但我无法弄清楚如何简单地替换所有 403响应("好吧,它存在,但你仍然必须在404中找到一种方式("抱歉,从未听说过").我希望有一个简单的解决方案,不需要更新正则表达式或.htaccess的其他位来匹配站点更改,只需一个简单的指令:"每当你决定返回403时,返回404代替"到整个站点,无论配置如何变化.
然后,如果顶级.htaccess包含"Options -Indexes",并且任何给定目录不包含index.html(或者equiv),则裸目录URL将返回404,但是如果我将index.html添加到该目录,相同的裸目录URL将返回index.html,不需要任何.htaccess文件的更新.
我甚至不关心,如果我曾经密码一个目录,一个错误的密码返回404(因为404 - > 403映射).在那种情况下,我没有通过返回404隐藏任何东西,但它也没有造成任何伤害.如果有办法取消对特殊情况的一般403-> 404映射(而不是特殊情况下的DO),那可能会更有用.
当然,如果我忽略了什么,请直截了当地说.
编辑:Drat.我试图在这里写一个质量好的问题,但我在第二段中对"选项 - 索引"行为的描述结果证明是错误的.没有该行,如果存在于目录中,则裸目录URL显示"index.html"; 否则,它会显示目录的内容.(如果index.html存在,将/ dir转发到/dir/index.html是Web主机的默认设置,除非我弄错了.)添加"Options -Indexes"行会阻止它在公共场所播放我的衣物(返回403,而不是404,但仍然比暴露dir内容更好),但现在即使index.html存在,裸目录URL也会返回403.
我希望一个裸露的目录URL"mysite.com/mydir"显示/mydir/index.html如果它存在,"404"如果它没有,但显然它不仅仅是用404替换403s.
我为phpmyadmin设置了以下设置:
<Directory /usr/share/phpmyadmin>
Options FollowSymLinks
DirectoryIndex index.php
Order Deny,Allow
Allow from 127.0.0.1
Deny from all
Run Code Online (Sandbox Code Playgroud)
...
</Directory>
Run Code Online (Sandbox Code Playgroud)
基本上,我只允许从localhost访问phpmyadmin.如果来自外界的人试图去http:// mydomain/phpmyadmin,他们将获得403(禁止).这可能会让他们知道它在那里,但他们无法达到它.
问题:我宁愿让Apache在这个例子中返回404.那可能吗?
我的.htaccess的相关部分看起来像这样:
Options -Indexes
<FilesMatch include>
Order allow,deny
Deny from all
</FilesMatch>
RedirectMatch 404 ^/include(/.*)$
Run Code Online (Sandbox Code Playgroud)
它产生了以下响应:
我可以通过查看我的模式来判断问题可能出现在(/.*)部分,但我尝试的所有内容都给出了相同的结果; 而不是一直得到404我得到一个404的404和其他一切的403.我正在使用的表达式有什么问题?或者,因为我必须为几个目录执行此操作,是否有一种全面的方法可以将所有403响应转换为404?
更新:我发现通过删除FileMatch我得到了更好的结果,所以我的.htaccess现在看起来像这样:
Options -Indexes
RedirectMatch 404 ^/include(/.*)?$ # Added dlamblin's first suggestion
Run Code Online (Sandbox Code Playgroud)
并生成以下响应:
更新:有趣的是,我发现以下产生不同的输出:
RedirectMatch 404 ^/include(/?|/.*)$
RedirectMatch 404 ^/template(/?|/.*)$
Run Code Online (Sandbox Code Playgroud)
模板模式适用于所有情况但是include仍然为include中的所有文件生成403(例如/include/config.inc)这可能是目录名称的问题而不是.htaccess文件本身的问题?
更新:访问/include/config.inc时,我的.htaccess中的以下内容与重定向冲突.
<FilesMatch config>
Order allow,deny
Deny from all
</FilesMatch>
Run Code Online (Sandbox Code Playgroud) apache .htaccess mod-rewrite http-status-code-403 http-status-code-404
这是一种后续行为是否有办法强制apache返回404而不是403?建议RedirectMatch404完成此任务.
对大多数事情都很好,但是如果我要求:
http://example.com/.htaccess
Run Code Online (Sandbox Code Playgroud)
如果我在我的.htaccess文件中有(或没有),我得到403而不是404 :
RedirectMatch 404 ^/\.htaccess$
Run Code Online (Sandbox Code Playgroud)
有没有办法从.htaccess文件本身内部覆盖默认的403 - apache的禁止行为?
我们目前有一个保护特定路径的 shibboleth 实现。然而,因为这个路径实际上是一个 HTTP 请求(由一个使用 $http 的 AngularJS 应用程序创建),shibboleth 会尝试将此请求“重定向”到身份提供者,但浏览器只是将其解释为无效请求。它以 status=-1 并且没有关联的标题/数据返回给 AngularJS。
我想拦截这个 302 并返回一个 401,并且最好能够编辑响应标头。有没有办法使用 Apache 或 Shibboleth 来做到这一点?
相关板块:
# Proxy all requests to WebLogic
<Location "/api">
SetHandler weblogic-handler
WLSRequest On
WebLogicHost services.endpoint.com
WebLogicPort 9002
</Location>
# For requests marked as protected, apply shibboleth
# If this block gets triggered, Shibboleth attempts redirect
# which does not work with our architecture
<Location "/api/protected">
AuthType Shibboleth
ShibRequireSession On
ShibApplicationId default
ShibExportAssertion On
Require Shibboleth
</Location>
Run Code Online (Sandbox Code Playgroud)
它是如何在 AngularJS 中使用的: …
apache ×4
.htaccess ×3
http ×2
ajax ×1
angularjs ×1
apache2 ×1
mod-rewrite ×1
security ×1
shibboleth ×1