Jesin's Blog

Welcome to the Portal of Technology

  • Facebook
  • GitHub
  • RSS
  • Twitter
  • Home
  • Categories
    • Domains
    • Linux
    • Networking
    • PHP
    • Virtualization
    • Web Design
    • Web Servers
    • Windows
  • WordPress Plugins
    • Custom Error Pages
    • HTTP Digest Authentication
    • Mailgun Email Validator
  • Toolbox
    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool
  • About Me
  • Contact Me
  • Sitemap
Home ›
Web Servers ›
How to create custom error page in Apache

How to create custom error page in Apache

August 20, 2010 Web Servers Jesin A 3 Comments

apache default error page

Web Designing can be fun and interesting. Using the current cutting edge of web programming courtesy HTML5, CSS3 and jquery a beginner can easily create mind blowing web templates that look cool and refreshing. But all the hours of work to decorate the face of your site is wasted if the user encounters a bland looking error page containing technical jargon that leaves visitors scratching their heads. So custom error pages are the order of the day!!!

Advantages of custom error pages

  • You can create an error page using the template you used to create your website. Thus even when someone encounters an error he/she will feel at home (or at your site)
  • Custom error pages boost your search engine ranking.
  • Since the error page is at your control you can write stuff that tells the user in plain English what caused the error.

So lets take a look on creating custom error pages on the mostly widely used open source web server software Apache HTTP server.

apache default error page
The default error page in Apache can be annoying and less informative for users

Step 1: Design the error page

I say design instead of create because you should use the same theme/template which the website uses for the error. The next man thing is that it should contain both technical and non technical information about the error. For example if you’re designing a “404 page not found” error page tell the user all the possibilities of arriving at this error. It could be due to an outdated link or a misspelt word or using a different case (lowercase or uppercase) for the web page name.

apache informative error page
Joomla's 404 error page shown here contains information that makes an user understand about the error which occured

Use the php code $_SERVER[‘REQUEST_URI’] to display the name of the non existing page/directory. Include a search box and tell users to search for what they were looking for. Tell them to also take a look at your Sitemap. Save the file as 404.html or any name that best describes the error. Pages can be created for the following common error codes

Error Code Reason
401 Authentication Required This error occurs if you are prompted to enter a valid username and password to view a page and you press cancel i.e. if you try to view a htpasswd or htdigest protected directory/file
403 Forbidden This error occurs if you try to view a file/directory that doesn’t have permissions to be viewed to the world. This includes htaccess, htpasswd files and directories forbidden from listing out their contents
404 Page Not Found If a non existing file or directory is requested this error is displayed
500 Internal Server Error Occurs when wrong syntax is used in htaccess file

Step 2: Edit the .htaccess file

Now that you’ve created the error page you should tell the web server to display it whenever an error is encountered instead of displaying its own default. So you need to create/edit your htaccess file. Search for it inside the public_html or www or htdocs folder. Don’t fret if it isn’t there just create a file named .htaccess (the dot at the beginning of the file is very important) Add the following line to that file.

ErrorDocument [error code] /[path to error page]

Example:-

ErrorDocument 404 /errors/404.html

Don’t forget to save the htaccess file.

Step 3: Testing the error page

Now for the fun part starts. Type your website’s url followed by some rubbish e.g. https://websistent.com/something Now if you’ve done everything correctly you should see your custom 404 error page. If you get a 500 Internal server error check for any syntax errors in the .htaccess file. If you see the error page file name, then its because you’ve omitted the slash in front of the file name path.

Bad practices for custom error pages

I’ve come across some websites which use their home page as an error page. Now this might seem simpler but just imagine the plight of a newbie who clicks an outdated link on that website and lands up on the home page. Such a person would be clueless on what has happened. Another bad practice is to show the website’s Sitemap.

Related posts:

Default ThumbnailUsing the Apache Redirect directive php category thumbnailHow to use custom php.ini files on shared hosting disable directory listing apache thumbnailDisable Directory Listing in Apache Default ThumbnailChange the default Directory Index in Apache Server wordpress custom 403 error pageCustom 403 and 401 error pages in WordPress

Tags: custom error pages, errordocument, htaccess

Trackbacks

  1. Apache Redirect directive | Jesin's Blog says:
    April 10, 2013 at 1:42 pm

    […] How to create custom error pages in Apache […]

    Reply
  2. Redirecting Wordpress archives to a page | Jesin's Blog says:
    June 28, 2013 at 1:16 pm

    […] “Location:” should the target page you mentioned in the header() function. If you get a 404 page not found error check the URL mentioned in the “Location:” header by copy pasting it into your […]

    Reply
  3. Using custom php.ini files on shared hosting | Jesin's Blog says:
    June 30, 2013 at 2:41 pm

    […] How to create custom error pages in Apache […]

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get a wealth of information delivered to your inbox. Subscribe and never miss a single article.

  • Tutorials and howtos
  • Code, scripts and commands
  • Online Tools

* No spam, unsubscribe anytime

Hire Me

  • SSL installation and hardening (A+ on Qualys SSL test)
  • Apache & Nginx configuration
  • Email deliverability improvement (10/10 on Mail Tester & MailGenius)
  • WordPress customization, optimization and migration
  • and much more…

    Tools

    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool

    Nav

    • Home
    • About Me
    • Contact Me
    • Privacy Policy
    • Sitemap
    Vultr SSD VPS

    Creative Commons License
    Jesin's Blog by Jesin A is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
    Based on a work at websistent.com.