简体   繁体   中英

.htaccess redirect from one domain to another including subdomains

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.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM