URL Re-writing in apache for multiple domain names
May 15th, 2009 | by Linux Hosting |Quite often a website should serve multiple names but should rewrite (or display in the location bar) only one of them. This situation arose at Bytehouse the other day where we had multiple domain names pointing to our webserver but we wanted all of them to display www.bytehouse.co.uk regardless of what had been typed into the browser. Using the apache mod_rewrite module this is easy enough to do. Here is the syntax added to our current configuration.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^bytehouse.co.uk [OR]
RewriteCond %{HTTP_HOST} ^(www.)?datacenterhosting.co.uk [OR]
RewriteCond %{HTTP_HOST} ^(www.)?datacentrespace.co.uk [OR]
RewriteCond %{HTTP_HOST} ^(www.)?virtual-rack-space.co.uk [OR]
RewriteCond %{HTTP_HOST} ^(www.)?virtual-rackspace.co.uk [OR]
RewriteCond %{HTTP_HOST} ^(www.)?virtulisation.co.uk [OR]
RewriteCond %{HTTP_HOST} ^(www.)?virtulization.co.uk
RewriteRule ^(.*)$ http://www.bytehouse.co.uk$1 [R=permanent,L]
As you can see this is fairly self explanatory. We set some conditions for rewriting based on the host header using a regular expression type syntax. You can see that by the ^(www.)? we are saying the www. portion of the host may not be in the header but we want to catch those URL’s aswell. The [OR] parameter is required otherwise a logical AND is assumed.
In the last line a rewrite rule is applied if any of the prior condtions are met. In this case a redirect is returned to the browser of www.bytehouse.co.uk The [R=permanent,L] at the end says that a “Permanent”(301) redirect should be returned and that this is last rewrite rule.
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.
Tags: multiple domains in apache, rewrites for multiple domains, setting up URLs for multiple domains, URL rewrites
