gpt4 book ai didi

.htaccess - "Spoofing".htaccess 未找到 404 错误

转载 作者:行者123 更新时间:2023-12-04 00:47:01 25 4
gpt4 key购买 nike

我目前已将 .htaccess 设置为通过以下方式重写任何没有句号或斜杠的等效内容(因此“foo”在内部拉出“foo.php”):

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !(.*)\.php
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/.]+)$ $1.php [L,NS]

但是,我希望对“foo.php”的直接请求返回 404,就好像该文件不存在一样(就像有人尝试过“foo.asp”或“foo.blargh”一样)。我知道有 [F] 和 [G] 标志,但我不想要“禁止”(403) 或“消失”(410),因为这些与不存在的文件返回的结果 (404) 不同。

有办法吗?

更新 1

我试过了

RewriteCond %{REQUEST_FILENAME} \.php$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.php\ HTTP/
RewriteRule ^(.*)$ /error.php?e=404 [R=404,L,NS]

有效...直到我尝试使用 ErrorDocument 处理程序。假设我的文档根目录中有“error.php”,任何类似 ErrorDocument 404/error?e=404 的内容(我也尝试过使用 error.php 等)都会反省错误

Not Found

The requested URL /foo.php was not found on this server.

Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

...但仅在尝试访问实际存在的 .php 页面时(例如 foo.php);任何访问实际上不存在的页面(因此不会触发我的第二个重写规则)的尝试都会转到由 ErrorDocument 处理程序指定的页面。

最佳答案

找到解决方案:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ (/.*)\.php
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} !^/error\.php
RewriteRule . - [R=404,L,NS]

ErrorDocument 404 /error.php?e=404

具体来说,第三个 RewriteCond 阻止对错误页面的请求触发 RewriteRule(因为 THE_REQUEST 仍然包含原始请求(匹配第一个 RewriteCond),并且错误页面确实存在(匹配第二个 RewriteCond)。

请注意,第三个 RewriteCond 与为 ErrorDocument 指定的 URI 格式匹配。在 error.php 中,我使用 $_SERVER["REDIRECT_SCRIPT_URL"] 找出用户最初请求的文件(因此看起来 404 是对他们请求的直接响应)。

关于.htaccess - "Spoofing".htaccess 未找到 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7630996/

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