如何将404重定向到index.html并重写URL为主页URL?

18

我将一个臃肿、复杂的网站改成了一个只有一页的小网站,因此用户需要从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。

3个回答

29

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

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

DirectoryIndex会使得http://domain.com/默认加载http://domain.com/index.htmlErrorDocument中使用http://将使其重定向到新的URL。


1
ErrorDocument 404 / - panticz
1
"ErrorDocument 404 /" 不会改变浏览器中的 URL。 - anubhava
1
对我来说,添加ErrorDocument就足够了。 - BrunoMartinsPro
这是否是一个好的实践?是否更合适(并且也许符合SEO规定)拥有美观的错误页面,并提供导航到网站可能重要的入口点? - s3c
@s3c:这个问题不是关于SEO的。有些内部网站可能不对公众开放。 - anubhava

4
尝试以下代码:

尝试以下代码:

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

ErrorDocument 404 /index.php

这是任何指向您的主页的404网址


使用重写行会为我创建一个重定向循环。仅使用ErrorDocument 404 /index.php似乎可以正常工作。 - Andrew S

-1
DirectoryIndex index.html  
ErrorDocument 404 http://example.com/

这个方法适用于损坏的链接。在.htaccess文件中应用,然后使用任何损坏的链接工具进行检查。


2
这本质上与已接受的答案相同。它添加了什么价值? - AlBlue

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接