Most efficient way to redirect several TLD's sharing a common root to one primary domain using htaccess

1.6k Views Asked by At

I'm using the following code (repeated) to redirect 6 additional TLDs to one primary TLD. is there a more efficient way to achieve the same result?

RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)?mydomain\.com$ [NC]
RewriteRule .?$ http://www.mydomain.com%{REQUEST_URI} [R=301,L]
2

There are 2 best solutions below

2
On BEST ANSWER
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.org$ [NC]
RewriteRule ^(.*)$ http://www.domain.org$1 [R=301,L]

This redirects any domain which is not .org to .org

0
On

If there are a lot of domains, you may want to look at RewriteMap which will allow you to create external maps, but this would need to be in your server/vhost config, won't work in an .htaccess file. Other than that, this is the usual way to do this.