【问题标题】:How do I redirect 404's to index.html and rewrite the URL to the home page URL?如何将 404 重定向到 index.html 并将 URL 重写为主页 URL?
【发布时间】:2014-12-17 01:57:42
【问题描述】:

我把一个庞大、复杂的网站改成了一个小的单页网站,所以需要将用户从 404 重定向到 index.html。

我把它放在 .htaccess 中:

ErrorDocument 404 /index.html

如果您键入 mydomain.com/lalalalala,这将重定向到主页内容 (mydomain.com/index.html),但 URL 栏仍显示 mydomain.com/lalalalala.

如何将 404 重定向到 index.html 并将 URL 重写为 mydomain.com

编辑:

我正在使用 Bluehost。

【问题讨论】:

    标签: html .htaccess redirect apache2


    【解决方案1】:
    DirectoryIndex index.html  
    ErrorDocument 404 http://example.com/
    

    这适用于断开的链接。申请.htaccess文件,然后检查任何损坏的链接工具。

    【讨论】:

    • 这与接受的答案基本相同。它增加了什么价值?
    【解决方案2】:

    试试下面的代码:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule . / [L,R=301]
    
    ErrorDocument 404 /index.php
    

    您的home page. 是任何404 url

    【讨论】:

    • 使用重写行为我创建了一个重定向循环。仅使用 ErrorDocument 404 /index.php 本身似乎工作得很好。
    【解决方案3】:

    您可以在 .htaccess 的顶部使用这两行:

    DirectoryIndex index.html
    ErrorDocument 404 http://domain.com/
    

    DirectoryIndex 将使http://domain.com/ 默认加载http://domain.com/index.html,在ErrorDocument 中使用http:// 将使其重定向到新的URL。

    【讨论】:

    • 或者只是:ErrorDocument 404 /
    • ErrorDocument 404 / 不会更改浏览器中的 URL
    • 对我来说添加 ErrorDocument 就足够了。
    • 对这是否是一个好的做法有任何见解吗?拥有漂亮的错误页面并导航到网站可能的重要入口点不是更合适(也许是 SEO?)?
    • @s3c:这个问题与 SEO 无关。可能有一些内部网站不向公众开放。
    猜你喜欢
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-29
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多