【问题标题】:Can't do a proper Redirect 301无法进行正确的重定向 301
【发布时间】:2016-07-25 18:51:22
【问题描述】:

我需要从旧域重定向 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 页。我需要将文件夹中的旧域重定向到新域根目录,并将所有页面和文件夹重定向到新域中的相应页面和文件夹。我怎样才能做到这一点?

【问题讨论】:

    标签: php .htaccess magento redirect mod-rewrite


    【解决方案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]
    

    【讨论】:

    • 我按照你告诉我的那样尝试过,但是当我尝试去例如 www.olddomain.com/folder/another-folder 时,它应该带我去 www.newdomain.com/new-folder /new-other-folder,而是将我带到 www.newdomain.com/folder/another-folder。我的大多数文件夹、子文件夹和产品名称在新域上都有点不同。请检查一下好吗?
    • 不幸的是,它保持不变。也许某些服务器配置有干扰?
    • 1.缓存已清除。 2.我忘了说是不同的主机。新的服务器/主机正在 nginx 上运行。
    • /folder/ 是否还有另一个 .htaccess?您的根 .htaccess 是否与我的回答中显示的完全一样?
    • 伙计,你拯救了我的一天!我的 .htaccess 在 /folder/ 中,根目录下还有另一个 .htacess。我把它改成了root,瞧!
    猜你喜欢
    • 2018-07-30
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    • 2015-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    相关资源
    最近更新 更多