Most efficient way to redirect several TLD's sharing a common root to one primary domain using htaccess
Question
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]
Solution
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
OTHER TIPS
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow