Search code examples
html.htaccesshttp-redirectapache2

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


I changed a bulky, complex website into a small one-page website, so users need to be redirected from 404s to index.html.

I put this in .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.

How do I redirect 404s to index.html and rewrite the URL to mydomain.com?

EDIT:

I'm using Bluehost.


Solution

  • You can use these 2 lines at the top of your .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.