繁体   English   中英

重定向到404页面

[英]Redirecting to a 404 page

我有一个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定制页面。 希望能有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM