4

I have a client that has a good amount of domain alias' and wants them all redirected to the one main domain on the site. They also want to know which of the domain alias' is doing the redirecting. I have that part down but I want to optimize the code to the best most proper way it should be and to eliminate the amount of code I have to write. I am wanting to know if there is a way to pass to the RewriteRule url the domain alias that was used.

This is what I have now. I am looking for the domain alias that is being hit and then passing that alias to the url. Then in google analytics I can see how many times that url was used to hit the page.

RewriteCond %{HTTP_HOST} ^(www\.)?domain-alias1\.com [NC]
RewriteRule ^(.*) http://www.main-domain.com/?domain-alias1\.com$1 [R=301,L}

But my goal is to not have to write both the condition and rule for every single domain alias.

Is there a way to see which alias was hit and then have the rewrite rule automatically add that to the position I have specified?

I had originally tried something like this just to see if it would work(although I have tried many different ways):

RewriteCond %{HTTP_HOST} ^(www\.)?([a-z]+)\.com [NC]
RewriteRule ^(.*) http://www.main-domain.com/?$1\.com$2 [R=301,L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
Rob
  • 113
  • 1
  • 7

1 Answers1

4

You can try something along these lines:

RewriteCond %{HTTP_HOST} !^(www\.)?main-domain\.com$ [NC]
RewriteRule ^(.*) http://www.main-domain.com/$1?domain=%{HTTP_HOST} [R=301,L]

With this any request NOT for domain www.main-domain.com will be redirected to www.main-domain.com with the domain name in query string domain.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Perfect! Exactly what I was looking for here. Not sure why I didn't even think about using the %{HTTP_HOST} in the rule. Thanks a lot. – Rob May 05 '11 at 21:15
  • Glad that it worked out for you. Whenever you can pls mark this answer as accepted to close this Q&A :) – anubhava May 05 '11 at 21:17