How do I use .htaccess conditional redirects for multiple domains?
- by John
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!