Adding a Custom Error Page when Hosting Multiple Sites Using Apache

Another optional extra you might want to consider when hosting multiple sites, or just when working with subdomains, is a custom error page.

Using the more basic configurations detailed in previous tutorials, you may occasionally find odd results being returned to your clients. For example, if someone were to simply type your server’s IP into their browser, or spelled a subdomain incorrectly, Apache2 will serve them up the first site it finds in its configuration path. If you’re hosting multiple sites, this means that a totally different site might be returned, which doesn’t exactly look professional.

Here, I’ll show you how to easily counter this problem, by creating a custom error page for your server.

[If you’re not familiar with Apache configuration files, read one of the tutorials linked above for more info.]

Step One

Create your custom error page and save it as ‘index.html’. In this example, we’re going to create the ‘index.html’ file at /home/randymarsh/errorpage/index.html.

This page could just be an image or some text, or you can write a redirect to one of your hosted sites or their subdomains. As long as it’s called ‘index.html’, you can do what you want here.

Step Two

Navigate to /etc/apache2/sites-available. We’re going to make a copy of an existing config file with the new name ‘custom-error’:

$ sudo cp randymarsh.com custom-error

Step Three

Open the file with your favourite text editor, we’re going to make some changes:

$ sudo nano custom-error

I’ve erased the ‘ServerAdmin’ entry, but there’s no reason for you to necessarily do this, it’s entirely up to you.

The key things to alter here:

  • Change ServerName to www.IP_OF_SERVER
  • Change ServerAlias to IP_OF_SERVER
  • DocumentRoot must point to the folder containing the custom ‘index.html’ file we created earlier

Step Four

It’s time again to enable your site, reload and restart Apache.

$ sudo a2ensite custom-error

$ sudo /etc/init.d/apache2 reload

$ sudo /etc/init.d/apache2 restart

And we’re done.

:::::EDIT:::::

This method has started to throw up some ‘interesting’ results when working with multiple sites. To get this to work correctly, you need to edit your DNS records with your registrar to allow wildcard subdomains! Otherwise you’ll get some very odd results. Live + learn!

Leave a Reply