用于重定向多个域和URI的最有效的htaccess规则

时间:2015-05-04 21:00:15

标签: .htaccess mod-rewrite redirect

我们正在合并一个主域名下的3个域名(oldsite1.com,oldsite2.com,primarysite.com)。来自旧域的内容已在主域上重现,但具有新路径。这两个旧域将更新其DNS记录,以便指向primarysite.com域的服务器,并且该服务器将配置为接受两个旧域的入站流量。使用primarysite.com帐户上的.htaccess文件进行重定向。大约有160个“路径”重定向规则。

因此,我们的重定向有两部分:

  1. 301将www.oldsite1.com和www.oldsite2.com的流量重定向至www.primarysite.com

  2. 301将特定路径重定向到新路径。

  3. 我的问题:对于160条路径中的每条路径,哪个更有效,单独的RewriteCond规则,例如:

    errno

    或者,最好是单个RewriteCond来处理两个域,然后是160个单独的RewriteRules,例如:

    RewriteCond %{HTTP_HOST} ^www.oldsite1.com$ [NC]
    RewriteRule ^old/path/to/page.html$ http://www.primarysite.com/new/path/ [L,R=301]
    
    RewriteCond %{HTTP_HOST} ^www.oldsite2.com$ [NC]
    RewriteRule ^old/path/to/otherpage.html$ http://www.primarysite.com/another/new/path/ [L,R=301]
    

1 个答案:

答案 0 :(得分:2)

这个更好,因为它是.htaccess中较少的代码:

# redirect specific paths to new server and new path
RewriteRule ^old/path/to/page\.html$ http://www.primarysite.com/new/path/ [L,R=301,NC]
RewriteRule ^another/path/to/different/page\.html$ http://www.primarysite.com/some/other/new/path [L,R=301,NC]

# else if primary domain is not primarysite then redirect to primarysite
RewriteCond %{HTTP_HOST} !^(www\.)?primarysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.primarysite.com/$1 [L,R=301]