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

牧云@^-^@ 提交于 2019-12-12 17:25:47

问题


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?


回答1:


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]


来源:https://stackoverflow.com/questions/44314863/htaccess-redirecting-multiple-domains-https-and-http-www-and-non-www-all-to-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!