如何将 404 重定向到 index.html 并将 URL 重写为主页 URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26493540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I redirect 404's to index.html and rewrite the URL to the home page URL?
提问by AnnaBlabber
I changed a bulky, complex website into a small one-page website, so users need to be redirected from 404s to index.html.
我把一个庞大、复杂的网站改成了一个小的单页网站,所以用户需要从 404s 重定向到 index.html。
I put this in .htaccess:
我把它放在.htaccess 中:
ErrorDocument 404 /index.html
If you type mydomain.com/lalalalala, this redirects to the home page content (mydomain.com/index.html), but the URL bar still says mydomain.com/lalalalala.
如果您键入mydomain.com/lalalalala,这将重定向到主页内容 ( mydomain.com/index.html),但 URL 栏仍显示mydomain.com/lalalalala。
How do I redirect 404s to index.htmland rewrite the URL to mydomain.com?
如何将 404 重定向到index.html并将 URL 重写为mydomain.com?
EDIT:
编辑:
I'm using Bluehost.
我正在使用 Bluehost。
回答by anubhava
You can use these 2 lines at the top of your .htaccess:
您可以在 .htaccess 的顶部使用这两行:
DirectoryIndex index.html
ErrorDocument 404 http://domain.com/
DirectoryIndex
will make http://domain.com/
load http://domain.com/index.html
by default and use of http://
in ErrorDocument
will make it redirect to new URL.
DirectoryIndex
将默认http://domain.com/
加载http://domain.com/index.html
并使用http://
inErrorDocument
将使其重定向到新的 URL。
回答by Dhrumin
Try below code :
试试下面的代码:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . / [L,R=301]
ErrorDocument 404 /index.php
It's any 404 url
to your home page.
404 url
对你有任何影响home page.