Reputation: 93
I have multiple domains with different TLDs (.com, .net, .org) but what's in front of the TLD is the same.
I would like to redirect the .net and .org to .com, without www:
www.<domain>.net
, <domain>.net
www.<domain>.org
, <domain>.org
www.<domain>.com
to be redirected to:
<domain>.com
(without www)<domain>
is dynamic and I don't want to hard-code it in .htaccess.
For redirecting www to non-www I use the following condition:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
I would like to extend this condition to include also the TLD condition and to achieve the 301 redirect in one step. I want to avoid two redirects as in:
www.<domain>.net -[301]-> <domain>.net -[301]-> <domain>.com
Upvotes: 1
Views: 2126
Reputation: 93
Starting from the idea posted by Croises, this is what I end up with:
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)\.(?:net|org)$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.(.+)\.(?:com)$ [NC]
RewriteRule ^(.*)$ http://%1.com/$1 [R=301,NE,L]
Upvotes: 0
Reputation: 18671
You can use:
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)\.(?:com|net|org)$ [NC]
RewriteCond %{HTTP_HOST} !^%1\.com$ [NC]
RewriteRule ^ http://%1.com%{REQUEST_URI} [R=301,NE,L]
Upvotes: 1