htaccess将多个域(https和http,www和非www)重定向到一个https域 [英] 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.
我想确保涵盖所有情况:如果用户键入http或https,或者是否包含www.
I want to make sure all cases are covered: if the user types http or https, if they include www or not.
这会创建12种不同的可能性...
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
我已经按照我认为应该适用于大多数变体的方式设置了.htaccess文件,并且我已经在Google和SO上进行了研究,这对我来说是最好的解决方案:
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]
但这仅涵盖了这些变体中的11个.
But this covers only 11 of these variations.
使用此htaccess文件,如果我访问URL:
With this htaccess file, if I visit the URL:
http://www.domain3.co.uk
它不会重定向.
所有其他变体都根据需要重定向.
All other variations redirect as required.
如何更改htaccess文件,以便涵盖所有12种可能的变化?
How can I change my htaccess file so that all 12 possible variations are covered?
推荐答案
您可以将此单个规则与[OR]
子句一起使用:
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]
这篇关于htaccess将多个域(https和http,www和非www)重定向到一个https域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!