Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 968
AnnaBlabber Avatar asked Oct 21 '14 18:10

AnnaBlabber


People also ask

How do I redirect a URL to index HTML?

The simplest way to redirect to another URL is to use an HTML <meta> tag with the http-equiv parameter set to “refresh”. The content attribute sets the delay before the browser redirects the user to the new web page. To redirect immediately, set this parameter to “0” seconds for the content attribute.

How do I redirect a URL to another URL?

Click the URL Redirects tab. In the upper right, click Add URL redirect. In the right panel, select the Standard or Flexible redirect type. A standard redirect is used to redirect one URL to another.

Should I redirect all 404 to homepage?

404s should not always be redirected. 404s should not be redirected globally to the home page. 404s should only be redirected to a category or parent page if that's the most relevant user experience available. It's okay to serve a 404 when the page doesn't exist anymore (crazy, I know).


2 Answers

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.

like image 161
anubhava Avatar answered Oct 30 '22 12:10

anubhava


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.

like image 20
Dhrumin Avatar answered Oct 30 '22 11:10

Dhrumin