htaccess: Redirecting multiple domains to the main domain

Let's say this is the main domain: mywebsite.com.au

And you'd like to redirect several other domains that you have purchased to this main domain.The steps shown below redirects all www, non-www, http and https variations and also matches the internal pages.

Step 1:
Go to your DNS provider's website and point each of those purchased domains to the server that is hosting your main domain. In other words, add the IP address of your hosting server in the A record.

Step 2:
Log in to your WHM. Then park each domain on top of the main domain.
You may be able to do this in cPanel > Aliases but I haven't tried.

Step 3:
If you have AutoSSL enabled, all the additional parked domains will automatically get SSL added. This may take some time. Alternatively, you can go to WHM > Manage AutoSSL and Run AutoSSL Check.

Step 4:
Add your parked domains to your .htaccess file as shown below.
Note the "$" at the end of this line: ^www\.mywebsite\.com$. This is to ensure it matches the .com domain only and doesn't take the .com.au domain. Otherwise it might try to redirect endlessly.

You may or may not need this line: RewriteCond %{HTTPS} off

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^mywebsite\.com [OR]
RewriteCond %{HTTP_HOST} ^www\.mywebsite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^mywebsite\.com\.au [OR]
RewriteCond %{HTTP_HOST} ^mywebsite\.org [OR]
RewriteCond %{HTTP_HOST} ^www\.mywebsite\.org [OR]
RewriteCond %{HTTP_HOST} ^myfavsite\.com\.au [OR]
RewriteCond %{HTTP_HOST} ^www\.myfavsite\.com\.au [OR]
RewriteCond %{HTTPS} off

RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\ Ballot169)?

RewriteRule ^(.*)$ https://www.mywebsite.com.au/$1 [R=301,L]

Comments

Popular posts from this blog

How to resolve 'res://ieframe.dll/acr_error.htm#' error (Internet Explorer stopped working while viewing a website)

MailChimp API V2.0 SSL error solution