How To Set Error Pages On Your Website Using .htaccess
Setting your error pages is important, so if someone tries to access a page that does not exist, or a page they are forbidden to see, they will get a friendly error message, plus some helpful links from you. Creating custom error pages, especially for the 404 (Not Found) error is a useful way to get lost visitors back on board.
All you have to do is add this code into your .htaccess file and save it! Like all file changes, it can take some time before it updates everywhere, but you can speed up the process on your devices simply by clearing your browser’s cache.
This code snippet will replace the default browser error page with your custom one. To create your own custom error page, create an web file (TXT, HTML, PHP, etc) with your custom message where your .htaccess file points to! For example, if you are using the code snippet below, create an HTML file in the "errors" folder named "404.html". When the server encounters a 404 error, it will redirect the visitor to your custom page instead of directing the browser to show a default one. Check out the entire list of error pages here.
Setup Custom Error Pages For Your Website
ErrorDocument 400 https://mysite.com/errors/400.html ErrorDocument 401 https://mysite.com/errors/400.html ErrorDocument 403 https://mysite.com/errors/403.html ErrorDocument 404 https://mysite.com/errors/404.html ErrorDocument 500 https://mysite.com/errors/500.html ErrorDocument 503 https://mysite.com/errors/503.html
Remember to replace the URL with your actual site URL, pointing to your custom error page!
Note for advanced users: According to Apache's doumentation, "If you use an ErrorDocument 4[xx] directive, then it must refer to a local document". To put in simple terms, it is recomended that you use relative links (Beginning with "/" for the root folder) in your htaccess file instead of absolute ones to prevent incorrect HTTP status messages from appearing. For example, replace "https://mysite.com/errors/400.html" with "/errors/400.html".
Unsure of how to install this code? It's pretty simple. A .htaccess file is called “.htaccess”, with nothing before the dot. Try finding this on your FTP software. Many systems don’t allow filenames starting with a dot (So downloading a htaccess file can be difficult at times), so that also restricts how we can create a .htaccess file. It is possible to get around these restrictions though, so here are the steps!
- Open your text-editing program and code your .htaccess file
- Once you are finished, save it as “htaccess.txt” (We will fix its name later)
- Upload “htaccess.txt” to your webserver and rename it to “.htaccess” (Remember that the dot is important!)
If you are still confused, or it's not working for you, check out the more detailed instruction in our Finding, Creating, and Editing a .htaccess file article.
Hopefully you were successful in installing this snippet, if you need help, feel free to send us a message! Check out the rest of the htaccess snippets to see what other amazing things you can do!