301 redirect to new domain and new name folder

…衆ロ難τιáo~ 提交于 2019-12-24 14:45:44

问题


I'm going to change my domain name but also need to change and one folder name on url structure. I want "www.example.com/name" to become "new-example.com/new-name".

I have this code for .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]

but this code remove "name" folder on redirected links. How can i change folder name and not remove it?


回答1:


Try this rule as your very first rule:

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]

Clear your browser cache before testing this change.




回答2:


Would this work?

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]


来源:https://stackoverflow.com/questions/36090241/301-redirect-to-new-domain-and-new-name-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!