Reputation: 772
I have some htaccess which redirects to www.domain.com unless its already on the www or www2 subdomain.
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^www2\.
RewriteRule (.*) http://www.%{HTTP_HOST}/$1
BUT this current bit of code doesn't strip off any current subdomain already on the host.
So what I'm looking for is some htaccess that will first strip off the subdomain and then add www. or a similar idea.
My other requirement though is that the domain name is dynamic as this same bit of htaccess is used on a number of sites. Which is why in my current bit of code I use the %{HTTP_HOST} variable. But maybe there's another variable that would work better for this case?
Upvotes: 2
Views: 975
Reputation: 60473
You could for example try matching for a www(2).domain.tld
scheme, capture the parts of the host with an additional condition for HTTP_HOST
, and use it in the rules replacement part, something like
RewriteCond %{HTTP_HOST} !^www2?\.[^\.]+\.[^\.]+$ [NC]
RewriteCond %{HTTP_HOST} ([^\.]+\.[^\.]+)$
RewriteRule (.*) http://www.%1/$1 [R=302,L]
This should redirect all non www(2).domain.tld
hosts to www.domain.tld
.
ps. Make sure sure to use the appropriate response status code depending on what you're trying to achieve with these redirects.
Upvotes: 1