.htaccess中的条件重定向

时间:2014-09-17 08:41:36

标签: .htaccess redirect

我不确定是否可以通过.htaccess实现我想要实现的目标,我已经没有任何运气来搜索Google了。

我有一堆重定向要应用于多商店网站,这意味着两个域将共享相同的.htaccess文件。我遇到的问题是每个域的重定向都是相互冲突的。

是否可以在.htaccess文件中设置等效的php'if'语句而不在每次重定向之前添加条件?

 <If domain = http://www.example-1.com>
 RedirectMatch 301 ^/en/1-2-drive-torque http://www.example-1.com/90-britool-expert-torque-wrenches
 RedirectMatch 301 ^/en/10-Britool http://www.example-1.com/24-britool-expert-spanners
 RedirectMatch 301 ^/en/11-Britool http://www.example-1.com/78-britool-expert-combination-spanners
 RedirectMatch 301 ^/en/12-Britool http://www.example-1.com/77-britool-expert-ratchet-spanners
 RedirectMatch 301 ^/en/13-Britool http://www.example-1.com/79-britool-expert-open-end-spanners
 etc...
 </If>

 <If domain = http://www.example-2.com>
 RedirectMatch 301 ^/en/1-2-drive-torque http://www.example-2.com/390-britool-expert-torque-wrenches
 RedirectMatch 301 ^/en/10-Britool http://www.example-2.com/624-britool-expert-spanners
 RedirectMatch 301 ^/en/11-Britool http://www.example-2.com/478-britool-expert-combination-spanners
 RedirectMatch 301 ^/en/12-Britool http://www.example-2.com/677-britool-expert-ratchet-spanners
 RedirectMatch 301 ^/en/13-Britool http://www.example-2.com/779-britool-expert-open-end-spanners
 etc...
 </If>

非常感谢任何帮助或建议

1 个答案:

答案 0 :(得分:0)

您需要使用mod_rewrite代替并使用%{HTTP_HOST}。不幸的是,没有办法在你的例子中创建像你一样的“if”容器。您每次都需要复制条件:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?example-1\.com$ [NC]
RewriteRule ^en/1-2-drive-torque /90-britool-expert-torque-wrenches [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example-1\.com$ [NC]
RewriteRule ^en/10-Britool /24-britool-expert-spanners [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example-1\.com$ [NC]
RewriteRule ^en/11-Britool /78-britool-expert-combination-spanners [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example-1\.com$ [NC]
RewriteRule ^en/12-Britool /77-britool-expert-ratchet-spanners [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example-1\.com$ [NC]
RewriteRule ^en/13-Britool /79-britool-expert-open-end-spanners [L,R=301]

RewriteCond %{HTTP_HOST} ^(www\.)?example-2\.com$ [NC]
RewriteRule ^en/1-2-drive-torque /390-britool-expert-torque-wrenches [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example-2\.com$ [NC]
RewriteRule ^en/10-Britool /624-britool-expert-spanners [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example-2\.com$ [NC]
RewriteRule ^en/11-Britool /478-britool-expert-combination-spanners [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example-2\.com$ [NC]
RewriteRule ^en/12-Britool /677-britool-expert-ratchet-spanners [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example-2\.com$ [NC]
RewriteRule ^en/13-Britool /779-britool-expert-open-end-spanners [L,R=301]