Apache Webserver: A Method to Change the Domain of Your Site CodeUnit 28 JAN 2013

Unfortunately domain names are seldom cast in stone when it comes to commercial products, because as with most things, people tend to change their minds further down the line.

If for the last three years your web application has been running under the domain myapp.com but now the boss has decided that it should rather be running under myawesomeapp.com, here is but one method that can help ease your pain in changing the domain of your site.

For this to work, we are assuming that you are running an Apache webserver which has the mod_alias module activated.

The first step would be to create a new virtualhost for the new domain (myawesomeapp.com) under the sites-available apache2 directory and enable it using the handy a2ensite script (if you are running Ubuntu Linux).

Next you would open up your existing site entry for myapp.com and change its contents as follows:

<VirtualHost *:80>
ServerName myawesomeapp.com
RedirectMatch 301 ^(.*)$ http://myawesomeapp.com
</VirtualHost *:80>

Save your changes and run the following in order to reload Apache’s configuration:

sudo /etc/init.d/apache2 reload

Breaking down what you’ve just done, basically you’ve instructed that for every request coming in to myapp.com, the server should redirect (with a permanent marker) to the new myawesomeapp.com domain but with the rest of the request string intact.

What the documentation tells us: This directive is equivalent to Redirect, but makes use of regular expressions, instead of simple prefix matching. The supplied regular expression is matched against the URL-path, and if it matches, the server will substitute any parenthesized matches into the given string and use it as a filename.


Related Link: http://httpd.apache.org/docs/2.2/mod/mod_alias.html#redirectmatch

Related Posts:

About Craig Lotter

South African software architect and developer at Touchwork. Husband to a cupcake baker and father to two little girls. I don't have time for myself any more.