Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm managing about 15 or so domains for a particular promotion. Each domain has specific redirects in place, as shown below. Rather than make 15 different .htaccess files that I would later have to manage separately, I'd like to use a single .htaccess file and use a symbolic link into each website's directory.

The trouble is that, I can't figure out how to make the rules apply only for a specific domain. Every time I visit www.redirectsite2.com, it sends me to www.targetsite.com/search.html?state=PA&id=75, when it should instead be sending me to www.targetsite.com/search.html?state=NJ&id=68.

How exactly do I make multiple RewriteRules apply for a given domain and only that domain? Is this even possible to do within a single .htaccess file?

Options +FollowSymlinks
# redirectsite1.com
RewriteEngine On
RewriteBase /

# start processing rules for www.redirectsite1.com
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} ^www\.redirectsite1\.com$

# rule for organic visit first
RewriteRule ^$ http://targetsite.com/search.html?state=PA&id=75 [QSA,R,L]
RewriteRule ^PGN$ http://targetsite.com/search.html?state=PA&id=26 [QSA,R,NC,L]
RewriteRule ^NS$ http://targetsite.com/search.html?state=PA&id=27 [QSA,R,NC,L]
RewriteRule ^INQ$ http://targetsite.com/search.html?state=PA&id=28 [QSA,R,NC,L]
RewriteRule ^AA$ http://targetsite.com/search.html?state=PA&id=29 [QSA,R,NC,L]
RewriteRule ^PI$ http://targetsite.com/search.html?state=PA&id=30 [QSA,R,NC,L]
RewriteRule ^GV$ http://targetsite.com/search.html?state=PA&id=31 [QSA,R,NC,L]
# catch-all rule, using the same id as the organic visit
RewriteRule ^([a-z]+)?$ http://targetsite.com/search.html?state=PA&id=75 [QSA,R,NC,L]

# end processing rules for www.redirectsite1.com

# begin rules for redirectsite2.com
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} ^www\.redirectsite2\.com$

# rule for organic visit first
RewriteRule ^$ http://targetsite.com/search.html?state=NJ&id=68 [QSA,R,L]
RewriteRule ^SL$ http://targetsite.com/search.html?state=NJ&id=6 [QSA,R,NC,L]
RewriteRule ^APP$ http://targetsite.com/search.html?state=NJ&id=8 [QSA,R,NC,L]
# catch-all rule, using the same id as the organic visit
RewriteRule ^([a-z]+)?$ http://targetsite.com/search.html?state=NJ&id=68 [QSA,R,NC,L]

Thanks for any help you may be able to provide!

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

One option would be to put the rewrite rules into different <VirtualHost> blocks. e.g.:

<VirtualHost *:80>
  ServerName www.redirectsite1.com
  RewriteEngine On
  RewriteRule ^$ http://targetsite.com/search.html?state=PA&id=75 [QSA,R,L]
  RewriteRule ^PGN$ http://targetsite.com/search.html?state=PA&id=26 [QSA,R,NC,L]
  RewriteRule ^NS$ http://targetsite.com/search.html?state=PA&id=27 [QSA,R,NC,L]
  RewriteRule ^INQ$ http://targetsite.com/search.html?state=PA&id=28 [QSA,R,NC,L]
  RewriteRule ^AA$ http://targetsite.com/search.html?state=PA&id=29 [QSA,R,NC,L]
  RewriteRule ^PI$ http://targetsite.com/search.html?state=PA&id=30 [QSA,R,NC,L]
  RewriteRule ^GV$ http://targetsite.com/search.html?state=PA&id=31 [QSA,R,NC,L]
  # catch-all rule, using the same id as the organic visit
  RewriteRule ^([a-z]+)?$ http://targetsite.com/search.html?state=PA&id=75 [QSA,R,NC,L]
</VirtualHost>

<VirtualHost *:80>
  ServerName www.redirectsite2.com
  RewriteEngine On
  # rule for organic visit first
  RewriteRule ^$ http://targetsite.com/search.html?state=NJ&id=68 [QSA,R,L]
  RewriteRule ^SL$ http://targetsite.com/search.html?state=NJ&id=6 [QSA,R,NC,L]
  RewriteRule ^APP$ http://targetsite.com/search.html?state=NJ&id=8 [QSA,R,NC,L]
  # catch-all rule, using the same id as the organic visit
  RewriteRule ^([a-z]+)?$ http://targetsite.com/search.html?state=NJ&id=68 [QSA,R,NC,L]
</VirtualHost>

Another option would be to put RewriteCond %{HTTP_HOST} ^www\.redirectsite1\.com$ before each and every RewriteRule for site 1 and RewriteCond %{HTTP_HOST} ^www\.redirectsite2\.com$ before each and every RewriteRule for site 2.

share|improve this answer
    
I'm going to go with the second option, which works really well, albeit a little less elegant looking. Thanks! –  John Feb 2 '11 at 15:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.