hcristea
hcristea

Reputation: 93

How to redirect, using .htaccess, multiple top level domains to one top level domain without www?

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:

  1. www.<domain>.net, <domain>.net
  2. www.<domain>.org, <domain>.org
  3. www.<domain>.com

to be redirected to:

<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

Answers (2)

hcristea
hcristea

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

Croises
Croises

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

Related Questions