如何对多个域使用 .htaccess 条件重定向?

如何对多个域使用 .htaccess 条件重定向?

我为某项促销活动管理了大约 15 个域名。每个域名都有特定的重定向,如下所示。我不想创建 15 个不同的 .htaccess 文件,以后再单独管理,而是想使用单个 .htaccess 文件并使用符号链接进入每个网站的目录。

问题是,我不知道如何让规则仅适用于特定域。每次我访问 www.redirectsite2.com,它都会将我发送到 www.targetsite.com/search.html?state=PA&id=75,而它应该将我发送到 www.targetsite.com/search.html?state=NJ&id=68。

我究竟如何才能使多个 RewriteRules 适用于给定域并且仅适用于该域?这甚至可以在单个 .htaccess 文件中完成吗?

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]

感谢您提供的任何帮助!

答案1

一种选择是将重写规则放入不同的 <VirtualHost> 块中。例如:

<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>

另一个选择是将其放在RewriteCond %{HTTP_HOST} ^www\.redirectsite1\.com$站点 1 的每个 RewriteRule 之前以及RewriteCond %{HTTP_HOST} ^www\.redirectsite2\.com$站点 2 的每个 RewriteRule 之前。

相关内容