Redirect Subdomain to new domain

人盡茶涼 提交于 2019-12-22 15:11:28

问题


Hi guys trying to get a 301 redirect working and having trouble. I need to redirect sub.domain1.com to www.domain2.com and make sure that any file names or parameters get sent over with it.

This is what I was trying:

RewriteCond %{HTTP_HOST} ^domain1.com [NC] 
RewriteRule ^(.*)$ http://www.domain2.com/$1 [L,R=301] 

I also tried this:

RewriteCond %{QUERY_STRING}  ^$
RewriteRule ^sub\.domain1\.com$ /www.domain2.com? [R=301,NE,NC,L]

Where am I messing up?


回答1:


Rule of thumb for rewriterules: from the most complex to the less complex. And don't forget the QSA directive (QSA = Query String Append = "make sure that any file names or parameters get sent over with it")

RewriteCond %{HTTP_HOST} ^sub\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [QSA,R=301,L]

Tell me if it works.




回答2:


You missed the subdomain part and proper escaping.

RewriteCond %{HTTP_HOST} ^sub\.domain1\.com$ [NC] 
RewriteRule ^(.*)$ http://www.domain2.com/$1 [L,R=301]

Further explain can be found in this question.



来源:https://stackoverflow.com/questions/8569399/redirect-subdomain-to-new-domain

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