htaccess 404 .htaccess
You have a few options for how to deal with 404 errors in htaccess such as setting a custom 404 error message or redirecting to a URL such as a custom 404 page or your home page. Here is how to set a custom 404 message, set a custom 404 page and redirect 404 errors to the homepage of your website in htaccess.
The default error message when a user visits a page that doesn't exist looks something like this.
Apache provides the
ErrorDocument directive which allows us to handle errors by displaying a message or redirecting to a URL. The Status of
ErrorDocument is Core, which means it is always available on an Apache web server. This means there is no need to do anything like
How to set a custom 404 error message with htaccess
Firstly, If you just want to display a custom error message to users who end up visiting a URL that doesn't exist, you can insert a custom error message directly in your htaccess file.
Here is how the above custom 404 error message would look in the browser.
You can also include HTML tags such as headings and paragraph tags in your custom error message directly in your htaccess file.
Here is how the above custom 404 error message with HTML tags would look in the browser.
How to redirect to a custom 404 page with htaccess
If you've created your own custom 404 page and want to redirect a user who visits a URL that doesn't exist to a custom 404 page, paste the URL of your custom 404 page after
ErrorDocument 404. This can be a relative URL such as "/404.html" or a full URL including the domain and protocol (https://).
If you are using the full URL, replace the domain in this example with your own domain and make sure that you use https if you have it enabled on your site, otherwise you can just use an http URL.
# or using the full URL path
ErrorDocument 404 https://example.com/404.html
This method allows you to use a 404 page that you've designed to match with your website's branding. You can be much more creative and fully design your page with a custom 404 page. Some custom 404 pages also have imagery and/or links to other pages.
If you provide the full URL of your custom 404 page, the user will see that exact URL such as "example.com/404.html", however if you just provide the relative path like "/404.html", the user will still be shown the same custom 404 page but their URL bar will show the page that they tried to reach such as "example.com/does-not-exist" so they will be able to check back at the URL to see which page doesn't exist. So for that reason, I would use the relative URL when redirecting to a custom 404 page.
If you use the full path to your custom 404 page and you have multiple websites on the same hosting and the other website(s) is/are located within their own directory/directories inside your main site's file structure, all sites will end up on your custom 404 page defined in the htaccess file in the root/main site directory, unless you also have an htaccess file within each website's root directory with their own 404 page URLs.
That is not ideal as you most likely want to keep each of your websites separate, so that is another reason I would use a relative URL for redirecting to a custom 404 page. Using a relative URL, means that the 404 error for each of your websites will be redirected to that website's own custom 404 page of the same name; all from just 1 htaccess file in the root directory of your main site, provided that your other sites are subdirectories within that and don't have their own htaccess files as they will overwrite ones in parent directories.
Or you could look at it as using a relative URL means you can add the same htaccess file to every website and it will work the same.
Either way, with 1 htaccess file in the root/parent directory for all sites or 1 htaccess file for each site, Just make sure you have created a custom 404 page for each site if you're redirecting to a custom 404 page, or the site(s) where you haven't will still just show the default error message because the 404 page you're redirecting to doesn't exist itself.
How to redirect 404 errors to homepage with htaccess
If you want to redirect all 404 errors to your website's home page, you can simply put a forward slash
ErrorDocument 404 or paste the full URL of your home page.
# or using the full URL path
ErrorDocument 404 https://example.com
This is an easy method but just redirecting 404 errors to the home page doesn't give the user any indication of what happened and why they are suddenly on the home page.
If you use the forward slash
/ method instead of using the full URL of your home page, the visitor may still see the page name that they tried to visit in the URL when they end up on the home page. For example, if you went to a page that doesn't exist called "does-not-exist" and in the htaccess ErrorDocument 404 rule you simply placed a forward slash
/, the user may see your home page but the url may still be something like https://example.com/does-not-exist.
In this case when redirecting to the home page instead of a custom 404 page, you may want to clean up the URLs by using full URL when redirecting to your home page to hide the path of the non-existent URL but there is a pretty large caveat.
Similarly to redirecting to a custom 404 page, if you have multiple websites on the same hosting nested in your main site's root directory, redirecting to the home page with a full URL will mean all of your sites redirect to your main site's home page, which is probably not what you want as you most likely want to keep them separate. By using a relative URL of just a forward slash
/, each site's 404 errors will redirect to that site's own home page, which makes much more sense and keeps your sites separate. That is unless you have a separate htaccess file within the other websites which will overwrite the rules in the parent directory's htaccess file. So I would use a relative URL for redirecting 404 errors to your website's home page as well.
For other redirects like if you've changed a page's location to a new URL and you want to forward the old URL to the new URL you can use 301 redirects with htaccess.
Thanks for reading.
Published on 22 Aug 2022