Setting a Custom 404 Page with htaccess

I bought a custom HTML website template with a 404.html page. How do I use it?

If I enter a wrong website address I don’t see the 404 page my browser simply shows a generic message from the hosting control panel.

The server needs to be configured to tell it what to do with when a page doesn’t exist.

The changes can be made in the .htaccess file found in the root of the public website.

Changes can be made either by FTP or via the file manager within the hosting control panel.

Often the file view begins one level down. Look for a directory called public_html, or similar.

In the .htaccess file add the following

ErrorDocument 404 /404.html

where 404.html is amended according to your error file name.

But this doesn’t work fully.. The error page is now shown as wished, but the address stays with the wrongly entered page.

What is required to complete is to redirect to the 404.html page. For this we’ll use the redirects.

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /404.html [R=301,L]

The above is added to the .htaccess file.

  • The first line turns on the redirects.
  • The following two lines set the rewrite conditions, checking for an existing file and directory respectively
  • Lastly the setting of the rewrite rule which takes the visitor to the 404.html page located in the root of the website.