R2D2
R2D2

Reputation: 2640

htaccess redirecting multiple domains, https and http, www and non-www, all to one https domain

I have three different domains, and I want to make sure that all domains redirect to the one domain.

I want to make sure all cases are covered: if the user types http or https, if they include www or not.

This create 12 different possibilities...

http://www.domain1.co.uk
https://www.domain1.co.uk
http://domain1.co.uk
https://domain1.co.uk

http://www.domain2.uk
https://www.domain2.uk
http://domain2.uk
https://domain2.uk

http://www.domain3.co.uk
https://www.domain3.co.uk
http://domain3.co.uk
https://domain3.co.uk

And I need all of these to redirect to:

https://www.domain3.co.uk

I have set up my .htaccess file in the way I thought should work with most variations, and I have researched this on Google and SO, and this looked to me to be the best solution:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain3\.co.uk$ [NC]
RewriteRule ^ https://www.domain3.co.uk%{REQUEST_URI} [R=301,L]

But this covers only 11 of these variations.

With this htaccess file, if I visit the URL:

http://www.domain3.co.uk

It does not redirect.

All other variations redirect as required.

How can I change my htaccess file so that all 12 possible variations are covered?

Upvotes: 3

Views: 2182

Answers (1)

anubhava
anubhava

Reputation: 785226

You can use this single rule with a [OR] clause:

RewriteEngine On

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.domain3\.co.uk$ [NC]
RewriteRule ^ https://www.domain3.co.uk%{REQUEST_URI} [R=301,L,NE]

Upvotes: 3

Related Questions