Kristo Vaher

inventas vitam iuvat excoluisse per artes

Skip to: Content | Sidebar | Footer

How to use multiple domains within one environment

4 May, 2010 (11:17) | Web Development | By: Kristo Vaher

This is a quick and unrelated post to all readers out there who pay for hosting services in shared virtualhost environments. I have been asked this question a few times in the past, but I figured it is good to share the tips here. In Estonia there are a number of hosting services for relatively low price, but one of the complications comes when you happen to own numerous domain names and would like these domain names to be run within the same environment. You could essentially run a number of WordPress blogs with entirely different domain names under the same hosting service.

What this needs is that all but one of your domain names should be an alias domain name for your main domain name. Most hosting services allow this without additional cost and in fact allow you to set it up yourself (should this be a problem, contact your hosting services customer support). You would then own a number of domain names for which you do still have to pay a fee (usually yearly), but you would only need to pay for only one hosting environment. Drawback of this is that the space and resources used by that environment would then be shared, but as long as you run small scale websites there (blogs and other sites for personal use), it will not become a problem.

To make this happen you need to set up a new index file on your web server which would be run first by PHP. In this index file you would detect the address that the user was trying to access and then direct them to the proper files or project. In case of wordpress you should rename its index.php to something like wpindex.php (alternative is to direct Apache to use another file for index, however many hosting services disable use of .htaccess commands). If I wanted my blog waher.net to always show my blog, but would like my other domain wearenixy.net work as an alias domain name for the domain without having to pay extra for hosting services I would set up something like this:

<?php
    if($_SERVER['SERVER_NAME']=='waher.net' || $_SERVER['SERVER_NAME']=='www.waher.net'){
        include('wpindex.php');
    } else if($_SERVER['SERVER_NAME']=='wearenixy.net' || $_SERVER['SERVER_NAME']=='www.wearenixy.net'){
        include('wearenixy.php');
    }
?>

If the user types in waher.net they would be redirected to wpindex.php and if the user is redirected through my waher.net alias name wearenixy.net, another script would be executed for that purpose.

This is essentially setting up your hosting service to work as a mini ‘domain routing’ service within your own virtualhost.

All the best!

Write a comment