11

Hello how do I redirect an error 404 to a home page with .htaccess?

Example: site.com if write site.com/some_site_notforund instead of 404 redirects us to the main page

Example 2:
sadistic.pl if write sadistic.pl/some_site_notfound instead of 404 redirects us to current page

sssss
  • 109
  • 1
  • 1
  • 10

5 Answers5

29

Try:

FallbackResource /index.html

or whatever the homepage is


try:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.html [L]
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • this is not work for my server. I looking some simular redirect to sadistic.pl if you write http://www.sadistic.pl/testowymasrekadaksdaksdad/ url stil work and not rederict to sadistic.pl sory for my english – sssss Feb 20 '14 at 04:37
  • @seweryns you'll need to use mod rewrite then, see edit above – Jon Lin Feb 20 '14 at 04:52
  • In which part of the `.htaccess` these lines must be placed? Inside the ``? Or outside? – Brethlosze Dec 06 '17 at 18:47
  • 2
    Best approach is - `ErrorDocument 404 /index.php` – Wolfack Jan 20 '20 at 19:53
16

You can do this like

this will be .htaccess file:

ErrorDocument 404 /404.php

All the page not found pages will display 404.php file.

In 404.php file you can write:

<?php
    header('location:index.php');
?>

So all page not found pages will go to 404.php page and it will redirect them to index.php

purush
  • 559
  • 3
  • 12
6

Or save a reroute step and just put the following into your .htaccess file:

ErrorDocument 404 /index.php

user7013200
  • 61
  • 1
  • 1
  • or ErrorDocument 404 http://www.yourdomain.com/index.php ( add the http : // infront pls ). It keeps being removed by SO – MarcoZen Jul 09 '18 at 07:29
4
  1. Make a .htaccess file in your root directory.
  2. Put this code into it and customize the RewriteRule.

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ http://127.0.0.1/dir/index.php [L]

Works perfectly on my localhost.

xereon
  • 66
  • 4
2

It works fine for my site. I tried this code, it doesn't show 404 pages, but it shows the same URL but content of the homepage

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.html [L]
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Jack
  • 29
  • 2