我试图为文档或文件夹、文件或链接显示自定义404页,这些文件或链接可能存在,也可能不存在,但出于某种目的,我仍然希望向访问者显示404的消息。
我检查了Setting a custom 404 page for a specific URL in htaccess和Setting a custom 404 page for a specific URL in htaccess
代码:
RewriteRule ^foo.html$ /spoof404.txt [L,R=404,NC]
RewriteRule ^hello$ /spoof404.txt [L,R=404,NC]
RewriteRule ^file/example.php$ /spoof404.txt [L,R=404,NC]使用这些代码,我成功地获得了404 Status in header,但它没有显示spoof404.txt的内容。
注意:
,
发布于 2022-01-10 16:32:22
我不是在找像404或403这样的错误纪念碑
为什么不行?你就是这么做的。在Apache上没有其他方法(不使用另一个服务器端脚本)触发404 HTTP状态并提供自定义响应。
例如:
ErrorDocument 404 /spoof404.txt
RewriteRule ^foo\.html$ - [NC,R=404]
RewriteRule ^hello$ - [NC,R=404]
RewriteRule ^file/example\.php$ - [NC,R=404]当您请求/foo.html时,它将触发404,而/spoof404.txt将被提供服务。
在3xx范围外指定状态ode时,将忽略替换字符串(因此使用了单个- -连字符)。在这种情况下,L标志也是多余的,因为它是在指定非3xx R代码时隐含的。
更新:
如何使用像
example.com/something.php?something=tokens&othertoken=token这样的参数进行阻塞
RewriteRule指令只与URL路径匹配,以匹配查询字符串(第一个?之后的所有内容),然后需要一个附加条件(RewriteCond指令),并检查QUERY_STRING服务器变量。
例如,要匹配准确的URL (尽管情况不敏感)并触发404:
RewriteCond %{QUERY_STRING} ^something=tokens&othertoken=token$ [NC]
RewriteRule ^something\.php$ - [NC,R=404]https://stackoverflow.com/questions/70655593
复制相似问题