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

时间:2021-12-31 07:42:21

We are consolidating 3 domains under a single primary domain (oldsite1.com, oldsite2.com, primarysite.com). The content from the old domains has been reproduced on the primary domain, but has new paths. The two old domains will have their DNS records updated so they point to the server of the primarysite.com domain, and that server will be configured to accept inbound traffic for the two old domains. Redirection will occur using an .htaccess file on the primarysite.com account. There are approximately 160 "path" redirection rules.

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

So, our redirection has 2 parts:

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

  1. 301 Redirect traffic for www.oldsite1.com and www.oldsite2.com to www.primarysite.com

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

  2. 301 Redirect specific paths to new paths.

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

My question: Which is more efficient, individual RewriteCond rules for each of the 160 paths, e.g.:

我的问题:对于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]

Or, is it better to do a single RewriteCond to handle both domains, followed by 160 individual RewriteRules, e.g.:

或者,最好是单个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 个解决方案

#1


This one is better as it is lesser code in .htaccess:

这个更好,因为它是.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]

#1


This one is better as it is lesser code in .htaccess:

这个更好,因为它是.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]