Got a website where you want to manage 404 error pages? Here’s how you can easily manage 404 errors on your website and redirect them to any desired page for better user experience.
When you are using WordPress, there are many plugins that will help you manage 404 errors by redirecting them to any desired page. It’s even better to use a plugin that redirects visitors from error 404 pages to the website home page with 301 error response rather than 404 error response. One such error 404 to 301 WP plugin is Link Juice Keeper.
Why Error 404 to 301?
In Internet language, 404 error response means the webpage does not exist. So, the website URL is treated as an error. When the response is 301 error, the URL is considered as to have existed previously and now move to an new location as provided with the error response. This means the URL is not treated as an error, but as to have simply moved to a new location.
When search engines such as Google get 404 error message, it comes to a dead end. When the error received is 301 (permanently moved) for a URL, the search engine will go to the new URL mentioned in the 301 response. This means any links pointing to any broken URL is now being redirected to the new URL, while retaining the link juice for better SEO.
How to Handle 404 Error via .htaccess?
If you prefer to have direct control, or do not use WordPress or any other content management system, you can define a custom 404 error page or URL using .htaccess in your PHP Apache based web hosting server.
If there is not .htaccess file in the main directory of your website, simply create a new one. The file name starts with a DOT, which means it’s a hidden file. If you have not turned on your web hosting panel preferences to show hidden files, turn it on and check before creating a new file with this name.
Define Custom 404 Page via .htaccess
Now, simply add the line below after replacing the website URL to your desired one.
ErrorDocument 404 http://yourwebsite.com/
The above code will start showing the URL defined whenever there is a 404 error response from your web server.
Define 404 to 301 via .htaccess
Just defining a custom 404 error page does not offer the SEO advantage. Hence, we need to tell the world that the error page is non-existent and has been permanently moved to a new location. Instead of giving 404 error response for non-existent URLs, our server will send a 301 error response with the new URL.
Here’s the BASIC CODE to be added in the .htaccess file to define all 404 error URLs to the homepage with 301 permanently moved response.
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteRule . / [L,R=301]
Simply adding the code above to the .htaccess file does the trick, but it only handles files & not folders.
Use the code below to properly handle folders & files that are present.
RewriteEngine On RewriteBase / # do not do anything for already existing files RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule .+ - [L] # return / if no such file or folder RewriteCond %{REQUEST_FILENAME} !-f RewriteCond ${REQUEST_FILENAME} !-d RewriteRule . / [L,R=301]
All your visitors now land on a valid page even when they type wrong or old URL. You also retain link juice via old or wrong URLs.