How to Redirect 404 Error Pages to URL using .htaccess


redirect 404 error pages to url using htaccess

If you are running a website, it is very likely that your users have encountered “404 : page not found” error at some point of time. Too many 404 error pages can decrease your search engine rankings and cause website visitors to leave. To fix this problem, you can redirect all the pages & folders that are not found, to a suitable page on your website, or even your home page. Here’s how you can easily redirect 404 error pages to URL using .htaccess file available in Apache web server.

 

How to Redirect 404 Error Pages to URL using .htaccess

Here are some common use cases for 404 error pages that you can redirect to URL. Basically, you need to open .htaccess file using a text editor and copy-paste the following commands, depending on your requirement. Remember to restart your Apache server (mentioned at the end) to apply the changes

 

Redirect a single page not found

If you have just a single page (e.g /old-url) that is not available, you can add the following line to .htaccess


redirect 301 /old-url/ http://www.yourdomain.com/new-url/

 

Redirect all 404 Error Pages

If you want to redirect all 404 error pages to, say, home page you can use the ErrorDocument directive


ErrorDocument 404 http://domainname.com

 

If you want to redirect all 404 error pages to a specific URL (e.g new-url)


ErrorDocument 404 http://domainname.com/new-url

 

You can also accomplish the same thing with a URL rewrite


<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

Once you have updated .htaccess file, please restart Apache server to apply the changes


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

Now Apache can redirect 404 error pages to URL using .htaccess 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!