How to 301 redirect an entire domain while preserving the path

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

I am redirecting one domain to another, but I want to preserve the path in the redirect. So for example, I want to visit www.example.com/services/education/page.html, but my redirect will bring them to www.new-example.com/services/education/page.html. What do I write in my .htaccess file to preserve the path "/services/education/page.html"?

Right now I have:

redirect 301 http://www.example.com/ http://www.new-example.com/ 

But I'm not sure if that works or not (Can't test yet as I am waiting for domain details etc). I just want to be sure when I put the site live. Is that right or am I way off base?

Thanks!

回答1:

This should do it:

RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} !new-example.com$ [NC] RewriteRule ^(.*)$ http://new-example.com/$1 [L,R=301] 


回答2:

try adding the following to your .htaccess in the root of your example.com domain

RewriteEngine On RewriteBase /  #for all requests to www.example.com RewriteCond %{HTTP_HOST} ^www\.example\.com$ #redirect them to new-example RewriteRule (.*) http://www.new-example.com/$1 [R=301,L] 


回答3:

Your original command uses the mod_alias Apache module, and it would work, though you may want to update it to:

Redirect 301 / http://www.new-example.com/ 

Removing the exact domain of the current (old) domain means all domains that point to that folder will be sent to the new domain, making that one-line script more robust.

The other answers use the mod_rewrite Apache module. If you have that also installed, that's fine to use, though it's 4+ lines of code compared to one. Additionally, mod_alias is part of the "base" package, so should be on all Apache servers, while mod_rewrite is an optional extension, so some might not have it.



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