301将代码重定向到新的文件夹结构URL

时间:2016-05-09 11:42:56

标签: php apache .htaccess redirect mod-rewrite

我在旧网站上使用此重定向:

RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^oldfolder(/.*)?$ http://newsite.com/newfolder$1    [L,R=301,NC,NE]

RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ http://newsite.com/$1 [L,R=301,NE]

此重定向代码可以帮助我:

  • 首先,将www重定向到我的新网站上的非www
  • 其次,使用oldfolder将oldsite网址重定向到包含newfolder的新网站

问题是一些旧的URL在其结构上有index.php而重定向是错误的。例如:

oldsite.com/index.php/oldfolder / ....

重定向到

newsite.com/index.php/oldfolder /....

但正确的重定向应该是

newsite.com/index.php/newfolder / ....或newsite.com/newfolder /....

如何更改重定向代码以对新文件夹结构网址进行正确的重定向?

1 个答案:

答案 0 :(得分:0)

您可以使用以下规则从oldsite /重定向到newsite:

RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.com$ [NC]
RewriteRule ^oldfolder/(.*)$ http://newsite.com/newfolder/$1    [L,R=301,NC,NE]