无法正确执行重定向301

时间:2016-04-05 17:18:17

标签: php .htaccess magento redirect mod-rewrite

我需要从旧域重定向301到新域。我在.htaccess中尝试了如下:

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^olddomain.com$[NC,OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$[NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*(default|index)\.(html|php|htm)\ HTTP/ [NC]
RewriteRule ^(([^/]+/)*)(default|main|index)\.(html|php|htm)$ http://www.newdomain.com/$1 [R=301,NC]

Redirect 301 /folder/ http://www.newdomain.com
Redirect 301 /folder http://www.newdomain.com
Redirect 301 /folder/another-folder http://www.newdomain.com/new-folder/new-other-folder
Redirect 301 /folder/another-folder/ http://www.newdomain.com/new-folder/new-other-folder
Redirect 301 /folder/product.html http://www.newdomain.com/product
Redirect 301 /folder/other-product.html http://www.newdomain.com/new-other-product
</IfModule>

但它没有被正确地重定向到上面代码中指定的每个页面或文件夹。我的旧网站在一个文件夹中。 我使用的是Magento,需要重定向大约200页。我需要将位于文件夹中的旧域重定向到我的新域根,并将所有页面和文件夹重定向到我的新域中的相应页面和文件夹。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

Redirect指令与RewriteRule混合使用。

您可以使用:

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*(default|index)\.(html|php|htm)\ HTTP/ [NC]
RewriteRule ^(([^/]+/)*)(default|main|index)\.(html|php|htm)$ http://www.newdomain.com.br/$1 [R=301,NC]

RewriteRule ^folder/?$ http://www.newdomain.com.br [L,NC,R=301]

RewriteRule ^folder/another-folder/?$ http://www.newdomain.com.br/new-folder/new-other-folder [L,NC,R=301]

RewriteRule ^folder/product\.html$ http://www.newdomain.com.br/product [L,NC,R=301]

RewriteRule ^folder/other-product\.html$ http://www.newdomain.com.br/new-other-product [L,NC,R=301]

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