301重定向到新域名和新名称文件夹

时间:2016-03-18 17:03:40

标签: php apache .htaccess redirect mod-rewrite

我要更改我的域名,但还需要在网址结构上更改一个文件夹名称。我想要" www.example.com/name"成为" new-example.com/new-name"。

我有.htaccess的代码:

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

但此代码删除" name"重定向链接上的文件夹。如何更改文件夹名称而不删除它?

2 个答案:

答案 0 :(得分:1)

这会有用吗?

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

答案 1 :(得分:0)

请将此规则作为您的第一条规则:

RewriteEngine On

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

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

在测试此更改之前清除浏览器缓存。