我有一个web应用程序,我想将用户重定向到404页面,而不让他们真正转到404页面。这可能吗?
我在.htaccess文件中添加了这行代码:
ErrorDocument 404 /404.php
因此,当用户输入(我的网站上不存在的页面)时:
shareit.me/jsdhjfkhoe
它们被重定向到:
shareit.me/404.php
有没有一种方法可以在URL保持不变的情况下重定向到404:
shareit.me/jsdhjfkhoe
如果404.php的所有路径不存在,则使用此方法将其传递给它们,并保留URL:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /404.php [L]
在404.php文件中设置此标头。
header("HTTP/1.0 404 Not Found");
此行:
ErrorDocument 404 /404.php
不会更改客户端浏览器中的URL,因为它只执行internal rewrite
到/404.php
我怀疑你的.htaccess中有其他规则可以进行完全重定向。如果你发布你的完整.htaccess,那么我可以调查。
实际上,这应该做到:
ErrorDocument 404 /404.html
->尝试在根目录中使用一个名为404.html的html元素!
如果您想看到一个完整的实现,请查看以下内容:https://github.com/h5bp/html5-boilerplate/blob/master/.htaccess
否则,如果有任何不清楚的地方,您可以查看文档:http://httpd.apache.org/docs/current/mod/core.html#errordocument
如果您想制作自己的自定义错误404页面。以下是您需要在.htaccess.上写下的内容
-----------.htaccess----------------
# 1 ---- Establish a custom 404 File not Found page ----
ErrorDocument 404 /filenotfound.php
# 2 ---- Prevent directory file listing in all of your folders ----
IndexIgnore *
-----------.htaccess----------------
其中filenotfound.php是您自己的404自定义页面。希望它能有所帮助。