Redirecting all domains to one domain using htaccess Print

  • htaccess
  • 2

Sometimes you will want to migrate all domains that point to the same webroot to a single domain name.

A real-life example would be, if you own a .com, .co.uk, .uk of the same domain (example.com/co.uk/uk) and want one of those domains to be the main domain everyone goes too.

The easiest way to handle this is with a .htaccess file as shown below, if you don't have a .htaccess file already in your webroot, just create a new file and name it '.htaccess' (with no other extension and nothing before the '.'):

Redirect with www and all to www.example.com

RewriteEngine On 
Rewritecond %{HTTP_HOST} !^www\.example\.com
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

Redirect without www and all to example.com

RewriteEngine On 
Rewritecond %{HTTP_HOST} !^example\.com
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

Was this answer helpful?

« Back