【问题标题】:How to redirect an error 404 to home page with .htaccess?如何使用 .htaccess 将错误 404 重定向到主页?
【发布时间】:2014-03-20 19:35:01
【问题描述】:

您好,如何将错误 404 重定向到带有 .htaccess 的主页?

示例: site.com 如果写成 site.com/some_site_notforund 而不是 404 会将我们重定向到主页

示例 2:
sadistic.pl 如果写入 sadistic.pl/some_site_notfound 而不是 404 会将我们重定向到当前页面

【问题讨论】:

  • 这就是它?错误文档 404 /index.html
  • 您是否已经将 AllowOverride 指令设置为 httpd.conf 以启用 .htaccess?
  • 是的,好的,我有这个代码 ErrorDocument 404 / witout index.html 并且可以工作
  • @Taizo ito 你如何在 ClearOS 网络服务器中允许覆盖?

标签: .htaccess redirect web http-status-code-404


【解决方案1】:
  1. 在您的根目录中创建一个 .htaccess 文件。
  2. 将此代码放入其中并自定义RewriteRule。
RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^ http://127.0.0.1/dir/index.php [L]

在我的本地主机上完美运行。

【讨论】:

    【解决方案2】:

    或者保存一个重新路由步骤,然后将以下内容放入您的 .htaccess 文件中:

    错误文档 404 /index.php

    【讨论】:

    【解决方案3】:

    你可以这样做

    这将是 .htaccess 文件:

    ErrorDocument 404 /404.php
    

    所有未找到的页面都会显示404.php文件。

    404.php文件中可以写:

    <?php
        header('location:index.php');
    ?>
    

    所以所有未找到的页面将转到 404.php 页面并将它们重定向到 index.php

    【讨论】:

      【解决方案4】:

      它适用于我的网站。我试过这段代码,它没有显示 404 页面,但它显示了相同的 URL 但主页的内容

      RewriteEngine On
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^ /index.html [L]
      

      【讨论】:

        【解决方案5】:

        试试:

        FallbackResource /index.html
        

        或者主页是什么


        尝试:

        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^ /index.html [L]
        

        【讨论】:

        • 这不适用于我的服务器。如果您写sadistic.pl/testowymasrekadaksdaksdad url stil 工作而不是重定向到sadistic.pl,我会寻找一些类似的重定向到sadistic.pl 对不起我的英语
        • @seweryns 你需要使用 mod rewrite 然后,请参阅上面的编辑
        • 这些行必须放在.htaccess的哪个部分?在&lt;IfModule mod_rewrite/c&gt; 里面?还是在外面?
        • 最好的方法是 - ErrorDocument 404 /index.php
        猜你喜欢
        • 2023-03-10
        • 2011-07-02
        • 2013-12-04
        • 2013-12-11
        • 2019-03-03
        • 1970-01-01
        • 2013-05-17
        • 2014-02-15
        • 2015-03-25
        相关资源
        最近更新 更多