【问题标题】:Redirecting to a 404 page重定向到 404 页面
【发布时间】:2013-12-02 18:12:51
【问题描述】:

我有一个网络应用程序,我想将用户重定向到 404 页面,而他们实际上不会进入 404 页面。这可能吗?

我将这行代码添加到我的 .htaccess 文件中:

ErrorDocument 404 /404.php

所以当用户输入(我的网站上不存在的页面)时:

shareit.me/jsdhjfkhoe

他们被重定向到:

shareit.me/404.php

有没有办法在 URL 保留时重定向到 404:

shareit.me/jsdhjfkhoe

【问题讨论】:

  • 问题解决了吗?很高兴有一个答案被接受为有效!

标签: php apache .htaccess redirect


【解决方案1】:

如果它们不存在,则使用它将所有路径传递给 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");

【讨论】:

  • 在不存在的页面中设置页眉?我怎么没想到呢!
  • 没有人建立(应该建立)可以有不存在页面的网站。所有页面都应该传递给一些代码文件,这些代码文件将识别页面并显示 404 标头的内容。
【解决方案2】:

这一行:

ErrorDocument 404 /404.php

不会更改客户端浏览器中的 URL,因为它只会将 internal rewrite 更改为 /404.php

我怀疑您的 .htaccess 中有其他一些规则来执行此完全重定向。如果您发布完整的 .htaccess,那么我可以进行调查。

【讨论】:

    【解决方案3】:

    实际上这应该可以解决问题:

    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

    【讨论】:

      【解决方案4】:

      如果您想制作自己的自定义错误 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 自定义页面。希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-01
        • 1970-01-01
        • 2011-12-05
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多