问题
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