18

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.

kjones
  • 1,339
  • 1
  • 13
  • 28
AnnaBlabber
  • 432
  • 1
  • 7
  • 20

3 Answers3

29

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.

anubhava
  • 761,203
  • 64
  • 569
  • 643
4

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.

Dhrumin
  • 351
  • 3
  • 16
  • Using the rewrite lines created a redirect loop for me. Just using ErrorDocument 404 /index.php by itself appears to work just fine. – Andrew S Jan 24 '20 at 18:31
-1
DirectoryIndex index.html  
ErrorDocument 404 http://example.com/

this would work for broken links. apply in .htaccess file and then check any broken link tool.

Zoe
  • 27,060
  • 21
  • 118
  • 148