gpt4 book ai didi

linux - 如何阻止直接访问我在 Apache 中的自定义 404 页面?

转载 作者:行者123 更新时间:2023-12-04 18:33:13 25 4
gpt4 key购买 nike

关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。












我们不允许在 Stack Overflow 上提出有关专业服务器或网络相关基础设施管理的问题。您可以编辑问题,使其成为 on-topic对于堆栈溢出。


1年前关闭。







Improve this question




我在 Ubuntu 中使用 Apache2 作为 Web 服务器,我的根目录是/var/www/html。在那里我有一个 .htaccess 文件

RewriteEngine on
ErrorDocument 404 /custom404.html
在里面。这行得通。当我访问 mydomain.com/arandomstring 时,我看到了 custom404.html 页面。但是,我想要做的是阻止对 custom404.html 的直接访问,例如 domain.com/custom404.html 应该不起作用。我将如何实现这一目标?我已经广泛搜索了 StackOverflow,但在这方面没有找到任何帮助。

最佳答案

您不能简单地阻止所有访问,因为自定义错误文档需要可访问才能被提供。
但是,您可以通过检查 REDIRECT_STATUS 来阻止直接访问。环境变量,在初始请求时为空,并在发生错误时设置为 HTTP 状态代码(例如,在 404 Not Found 的情况下为“404”)。
例如,在 .htaccess 中使用 mod_rewrite :

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^custom404\.html$ - [F]
这在 /custom404.html 时提供 403 Forbidden是直接要求的。或更改 FR=404改为提供 404 Not Found(不触发重写循环)。
更新:如果在服务器(或虚拟主机)上下文中使用,则需要在 RewriteRule 上添加斜杠前缀。图案。例如: RewriteRule ^/custom404\.html$ - [F]
    <Location /custom404.html>
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteRule .* - [R=404]
</Location>

像这样写(下面的代码)会更简单(并且效率略高)(不需要 <Location> 包装器):
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteRule ^/custom404\.html$ - [R=404]

关于linux - 如何阻止直接访问我在 Apache 中的自定义 404 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65472303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com