301重定向到更改的网址
问题描述:
我需要改变我的网站的域名,我会用永久重定向301.我发现,这种设置上的.htaccess将做的工作:301重定向到更改的网址
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://www.new-example.com/$1 [L,R=301,NC]
但我也需要将所有网址结构从http://www.new-example.com/old/this-is-url1.html更改为http://www.new-example.com/this-is-url1.html。
规则必须在他们的别名效果仅网址目录old
:www.new-example.com/old/...
而且他们重定向到URL,而不目录old
:www.new-example.com/
你能告诉我什么重定向规则我必须使用.htaccess来实现这种情况吗?测试这种变化之前
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(?:old/?)?(.*)$ http://new-example.com/$1 [L,R=301,NC,NE]
清除浏览器缓存:
答
这一规则替换您的代码所示。
此代码完美适用于更改我所有与“旧”目录的链接,但我还需要更改所有带有新域名的链接。使用两个代码是否正确?我的第一个域名重定向代码和您的代码是否适合我的具体情况? – Haris
是的,这是一个完美的工作!我使用:RewriteEngine on RewriteCond%{HTTP_HOST} ^(www \。)?example \ .com $ [NC] RewriteRule ^(?: old /)?(.*)$ http:// new-example。 com/$ 1 [L,R = 301,NC,NE] – Haris
此代码完美无缺!我尝试使用没有www的rewriterule,让我所有的链接都没有www,并且工作完美,除了我使用www进入新域名的情况。当我用www输入新的域名时,它不会将其重定向到非www。 – Haris