首页 文章

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

提问于
浏览
1

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

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

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

  • 301将特定路径重定向到新路径 .

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

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]

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

RewriteCond %{HTTP_HOST} !www.primarysite.com$ [NC]
RewriteRule ^(.*)$ http://www.primarysite.com/$1 [R=301]

RewriteRule ^old/path/to/page.html$ http://www.primarysite.com/new/path/ [L,R=301]
RewriteRule ^another/path/to/different/page.html$ http://www.primarysite.com/some/other/new/path [L,R=301]

1 回答

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

相关问题