有没有办法强制apache返回404而不是403?
有没有办法将 Apache Web 服务器配置为返回 404(未找到)错误代码,而不是对于我想要禁止访问的某些特定目录返回 403(禁止)?
我发现一些建议使用mod_rewrite的解决方案,例如
RewriteEngine On
RewriteRule ^.*$ /404 [L]
发送404而不是403的目的是为了混淆目录结构,这个解决方案太暴露了,因为它重定向到一些不同的位置,这使得很明显最初访问的目录事实上确实存在。
Is there a way how I can configure the Apache web server to return a 404 (not found) error code instead of 403 (forbidden) for some specific directories which I want to disallow to be accessed?
I found some solutions suggesting the use of mod_rewrite, like e.g.
RewriteEngine On
RewriteRule ^.*$ /404 [L]
As the purpose of sending 404 instead of 403 is to obfuscate the directory structure, this solution is too revealing, because it redirects to some different location which makes it obvious that the directory originally accessed does in fact exist.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
RedirectMatch 如例如
所示,它禁止访问所有以点开头的文件或目录,都会出现“404 Not Found”错误。
Apache 手册中写道:“Redirect[Match] 指令通过要求客户端在新位置重新获取资源,将旧 URL 映射到新 URL。”默认情况下, Redirect 发送 302 返回代码,但它还可以返回其他状态代码,如上所示。
RedirectMatch as in e.g.
does the trick, it prohibits access to all files or directories starting with a dot, giving a "404 Not Found" error.
From the Apache manual: "The Redirect[Match] directive maps an old URL into a new one by asking the client to refetch the resource at the new location." By default, Redirect sends a 302 return code, but it can also return other status codes as shown above.
您可以制作如下内容:
.htaccess
404.php
You can make something like this:
.htaccess
404.php
遇到同样的问题后,我最终得到了以下 .htaccess 文件。
第一行和第三行确保您无法列出文件夹内容,如果这样做,您将收到 404 错误。
RewriteCond 指令确保此重写规则仅适用于主域。由于我有几个子域,没有 rewritecond,访问 www.mydomain.com/subdomain 也会返回 404,这不是我想要的。
After having the same problem, I ended up with the following .htaccess file
The 1st and 3rd line ensure that you can't list the folder content, and if you do it you will receive a 404 error.
The RewriteCond directive ensures that this rewrite rule only applies to main domain. Since I have several subdomains, without the rewritecond, accessing www.mydomain.com/subdomain was also returning a 404, which was not what I intended.
在我看来,在 .htaccess 中执行此任务是相当丑陋的解决方案。
可以在 apache 配置中进行。看一下:
添加到您的 apache 配置:
然后重新启动 apache:
就这样。
In my opinion making this task in
.htaccess
is quite ugly solution.It is possible to make it in apache configuration. Take a look:
Add to your apache config:
Then restart apache:
That is all.