可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.