how do I setup custom error pages using htaccess 2 0

Last updated on Dec 25, 2022 17:44 in Development
Posted BySinhcoms LLP

Add some extra bit of flare to your website with custom error pages. 404 errors or 501 errors, customize them all. Maybe even give your users a useful link or just some clever word play. Creating these sweet display pages for your users is quick and easy with your .htaccess file. Best part? One line of code per error page. It is so simple!

Different Types of Error Pages

  • 404 error: Page not found – perhaps the most common error page, the 404 error is given when the web page or file you are trying to access does not exist. Like if you fat-fingered the folder name or if the file was deleted off the server.
  • 403 error: Forbidden – error given when the access to the specific file or folder is unauthorized. Commonly used for when an administrator wants to restrict access to an entire folder.
  • 401 error: Unauthorized – this error is given when the user is trying to access a page or folder that is restricted and they do not have permission for. This can also be given after multiple failed login attempts.
  • 500 error: Internal server error – this is a generic error message that a web server will give for any of the internal errors that it may receive, (e.g. server being overloaded or a coding error in some files).

How to get custom error pages

To get your own error pages for your site, just open up your .htaccess file for editing. Once it is open, you just need to add one line of code per error page.

Create your custom error pages and save it as a separate HTML file (e.g. 404.html). I usually like to save all of my custom error pages into a folder for just the error pages (e.g. “errors”). It just makes my life more organized, you know?

Open your .htaccess file and add the following line of code to it:

ErrorDocument 404 /errors/404.html

You can easily add additionally custom error pages by listing the error code number and the relative location of the html file for that error. Like this:

ErrorDocument 500 /errors/500.html




BEWARE These error pages will only work for servers generated errors, not for scripts like PHP generated server errors. ERROR CODES: 2xx Success Error Code List ======================= 200 – OK 201 – Created 202 – Accepted 203 – Non-Authoritative Information (since HTTP/1.1) 204 – No Content 205 – Reset Content 206 – Partial Content (RFC 7233) 207 – Multi-Status (WebDAV; RFC 4918) 208 – Already Reported (WebDAV; RFC 5842) 226 – IM Used (RFC 3229) 3xx Redirection Error Code List ========================= 300 – Multiple Choices 301 – Moved Permanently 302 – Found 303 – Check Other (since HTTP/1.1) 304 – Not Modified (RFC 7232) 305 – Use Proxy (since HTTP/1.1) 306 – Switch Proxy 307 – Temporary Redirect (since HTTP/1.1) 308 – Permanent Redirect (RFC 7538) 4xx Client Error Code List ===================== 400 – Bad Request 401 – Unauthorised (RFC 7235) 402 – Payment Required 403 – Forbidden 404 – Not Found 405 – Method Not Allowed 406 – Not Acceptable 407 – Proxy Authentication Required (RFC 7235) 408 – Request Timeout 409 – Conflict 410 – Gone 411 – Length Required 412 – Precondition Failed (RFC 7232) 413 – Payload Too Large (RFC 7231) 414 – URI Too Long (RFC 7231) 415 – Unsupported Media Type 416 – Range Not Satisfiable (RFC 7233) 417 – Expectation Failed 418 – I’m a teapot (RFC 2324) 421 – Misdirected Request (RFC 7540) 422 – Unprocessable Entity (WebDAV; RFC 4918) 423 – Locked (WebDAV; RFC 4918) 424 – Failed Dependency (WebDAV; RFC 4918) 426 – Upgrade Required 428 – Precondition Required (RFC 6585) 429 – Too Many Requests (RFC 6585) 431 – Request Header Fields Too Large (RFC 6585) 451 – Unavailable For Legal Reasons 5xx Server Error Code List ====================== 500 – Internal Server Error 501 – Not Implemented 502 – Bad Gateway 503 – Service Unavailable 504 – Gateway Timeout 505 – HTTP Version Not Supported 506 – Variant Also Negotiates (RFC 2295) 507 – Insufficient Storage (WebDAV; RFC 4918) 508 – Loop Detected (WebDAV; RFC 5842) 510 – Not Extended (RFC 2774) 511 – Network Authentication Required (RFC 6585)

Video
** The time is base on America/New_York timezone