2

Is is possible that i can configure RewriteRule for more then one domain.

Like my requirement is My current domain name www.maindomain.com and let say i have three domian and subdomain the subdomain url is example1.maindomain.com example2.maindomain.com example3.maindomain.com

Now i want when ever user try to access www.example1.com it should get the content of example1.maindomain.com and the same for example2, example3

I am using apache + passenger.

Thanks for help.

earl
  • 40,327
  • 6
  • 58
  • 59

1 Answers1

10

First, write a condition that matches all the domain names you want to redirect. Using the matched part of the domain, write a rule that rewrites to the target subdomain URLs. So, given the desired mapping stated in your question, something like the following should do the trick:

RewriteCond %{HTTP_HOST} ^www\.(example[123])\.com$ [NC]
RewriteRule ^(.*) http://%1.maindomain.com/$1 [L,R]

The above rewrites from e.g. www.example1.com to example1.maindomain.com. Similarly, if you need it the other way round:

RewriteCond %{HTTP_HOST} ^(example[123])\.maindomain\.com$ [NC]
RewriteRule ^(.*) http://www.%1.com/$1 [L,R]

This would rewrite e.g. example2.maindomain.com to www.example2.com.

earl
  • 40,327
  • 6
  • 58
  • 59
  • Can this be done using a wildcard? Something like: RewriteCond %{HTTP_HOST} ^(*)\.maindomain\.com$ [NC] RewriteRule ^(.*) http://www.maindomain.com/$1 [L,R] – tvgemert Nov 23 '12 at 16:10
  • 1
    The above actually already contains a "wildcard": the hostname pattern in the [`RewriteCond`](http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#RewriteCond) is a regex, which matches `www.example1.com`, `www.example2.com`, and `www.example3.com`. You can of course use use any other regex as well. For matching any subdomain(s), `\.maindomain\.com$` should do (or, alternatively `^.*\.maindomain\.com$`). – earl Nov 26 '12 at 09:36
  • Thanks! What I've found working is: RewriteEngine On RewriteCond %{HTTP_HOST} ^(.+)\.domain\.nl$ [NC] RewriteRule ^ http://domain.nl/?subdomain=%1 [L,R] – tvgemert Nov 26 '12 at 10:16