How To Redirect 404 Error Page To Home Page


redirect 404 error page to home page

When you run a website, it is likely that your users encounter “404 : page not found” error, when you delete or move some of your existing web pages. If you don’t something about them, then it will not decrease your website’s search rankings but also drive away visitors. One of the solutions to this problem is to simply redirect 404 error page to home page of your website. Let us look at how to redirect 404 error page to home page.

 

How To Redirect 404 Error Page To Home Page

Here are different ways to redirect 404 error page to home page. Basically, you need to open .htaccess file using a text editor and paste the following commands. Remember to restart your Apache server to apply the changes.

 

Open the .htaccess file

.htaccess file allows you to set up URL rewrites and redirects without accessing Apache’s server config files. Open it with a text editor. You’ll find it in the root folder of website (e.g /var/www/html in Linux). If you can’t find it, you can create it. Just name it .htaccess. Nothing before or after.

 

 

Using ErrorDocument Directive

Apache provides an ErrorDocument directive that allows you to direct all 404 error pages to a specific page, such as your home page


ErrorDocument 404 http://domainname.com

 

Using URL Rewrite

You can accomplish the same with URL Rewrite directive


<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

 

Restart Apache Server

Remember to restart Apache Server after you have made the changes


$ sudo systemctl restart apache2 [Ubuntu/Debian]
$ sudo systemctl restart httpd [RHEL/CentOS]

 

Now Apache will redirect 404 error page to home page automatically.

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!