How to redirect a URL by only changing the domain name, while keeping other URL parameters

拟墨画扇 提交于 2020-01-01 12:25:08

问题


I'm now migrating my website to a new host and domain, and I want to know if I can redirect anyone who enters any URL of the old website to the new website, while keeping all of the URL parameters. for example:

When somebody types in this url http://www.domainA.com/blog/?p=667, I want him to be redirected to http://www.domainB.com/blog/?p=667.

Is there any way I can do that by adding some .htaccess configurations?

Thanks!


回答1:


Try this in your .htaccess file:

Options +FollowSymlinks -MultiViews
RewriteEngine on

# for http
RewriteCond %{HTTP_HOST} ^(www\.)?domainA\.com$ [NC]
RewriteCond %{SERVER_PORT} =80
RewriteRule ^(.*)$ http://www.domainB.com/$1 [R=301,L]

# for https
RewriteCond %{HTTP_HOST} ^(www\.)?domainA\.com$ [NC]
RewriteCond %{SERVER_PORT} =443
RewriteRule ^(.*)$ https://www.domainB.com/$1 [R=301,L]

This will preserve your URI while redirecting from domainA to domainB.



来源:https://stackoverflow.com/questions/5956844/how-to-redirect-a-url-by-only-changing-the-domain-name-while-keeping-other-url

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