如何将 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:55:04  来源:igfitidea点击:

How do I redirect 404's to index.html and rewrite the URL to the home page URL?

html.htaccessredirectapache2

提问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/

DirectoryIndexwill make http://domain.com/load http://domain.com/index.htmlby default and use of http://in ErrorDocumentwill 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 urlto your home page.

404 url对你有任何影响home page.