3

I have two domains, for example:

  • example.com
  • website.co.uk

I want to redirect in the following ways:

  • example.com --> https://www.website.co.uk
  • alt.example.com --> https://www.alt.website.co.uk
  • test.example.com --> https://www.test.website.co.uk

As you can see, I want it always to force https and www.

But, I also want it to remember the rest of the URL strcture, for example:

  • example.com/folder/index.html --> https://www.website.com/folder/index.html
  • alt.example.com/test/ --> https://www.alt.website.com/test/
  • test.example.com/o/index.php --> https://www.test.website.com/o/index.php

I'm not sure the best way for this to be done, the two options I found were to try rewrite rules and the other was a 301 redirect.

Ryflex
  • 5,559
  • 25
  • 79
  • 148
  • 1
    Possible duplicate of [htaccess redirect for non-www both http and https](http://stackoverflow.com/questions/2015159/htaccess-redirect-for-non-www-both-http-and-https) – Sumurai8 Dec 20 '15 at 11:41
  • (yes, the duplicate preserves the protocol instead of forcing https, but this is the simpler case of the two. Just remove the condition and set the protocol to https) – Sumurai8 Dec 20 '15 at 11:43
  • No, not a duplicate of that because I want it to force the www and https because otherwise there will be 3 redirects as my main site already forces www and https – Ryflex Dec 20 '15 at 12:08
  • There is only 1 redirect in the linked question. It is not a perfect duplicate, but the changes are soo minor that I feel like I have to plagiarize the answers there to answer this question. If you understand the directives there, the changes to make your requirements work should be trivial. – Sumurai8 Dec 20 '15 at 12:12

1 Answers1

3

Try the following rules

RewriteEngine on


#redirect subdomains
RewriteCond %{HTTP_HOST} ^((?!www).+)\.domain.com$
RewriteRule ^(.*)$ http://%1.domain2.com/$1 [L,R]

#redirect domain

RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$
RewriteRule ^(.*)$ http://www.domain2.com/$1 [L,R]
Amit Verma
  • 40,709
  • 21
  • 93
  • 115