How do I redirect all subdomain requests to primar

2019-07-13 10:53发布

I need to redirect all subdomain requests to be redirected to my primary domain in my htaccess. I need it to also include some sort of wildcard redirect.

e.g. washington.mysite.com/ redirect to mysite.com/washington

but I need to to also redirect any old url on the subdomain to mysite.com/washington

e.g. washington.mysite.com/category.aspx?washington-attractions&CatID=17 redirect to my-site.com/washington

This is my code so far:

RewriteCond %{HTTP_HOST} ^washington\.mysite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.washington\.mysite\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.mysite\.com\/washington/$1" [R=301,L]

However it still appends the old URL to the new one

e.g. washington.mysite.com/category.aspx?washington-attractions&CatID=17 redirects to my-site.com/washington/category.aspx?washington-attractions&CatID=17

Basically I need the redirect washington.mysite.com/*(anything) to my-site.com/washington

Any suggestions would be much appreciated :)

标签: .htaccess
1条回答
闹够了就滚
2楼-- · 2019-07-13 11:39
RewriteCond %{HTTP_HOST} ^(www\.)?washington\. [NC]
RewriteRule ^(.*)$ http://www.mysite.com/washington [R=301,L]

In your RewriteRule you have /$1 which matches the (.*) wildcard set of parentheses. This is why you're getting the path from the old URLs appended.

I combined your 2 RewriteCondition's, making the (www\.) match optional with ?.

The [NC] flag is the nocase flag.

To clear the querystring, append ? to the end of the rewrite URL

RewriteCond %{HTTP_HOST} ^(www\.)?washington\. [NC]
RewriteRule ^(.*)$ http://www.mysite.com/washington? [R=301,L]
查看更多
登录 后发表回答