.htaccess将所有请求重定向到非www https域


.htaccess redirect all requests to non www https domain?

我知道关于堆栈溢出的问题已经很多了,但我找到的解决方案似乎都无法正常工作。

我已经设置了一个SSL证书,要求它所在的域是非www。

我想将访问我网站的用户重定向到https://example.com无论他们来自哪里:

http://example.com变为https://example.com

http://www.example.com变为https://example.com

https://www.example.com变为https://example.com

应该很简单,但不幸的是,我没有访问知识。我感谢任何帮助!

您可以在DOCUMENT_ROOT/.htaccess文件中使用以下代码:

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond ^www'. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,NE,L]

以下规则集满足以下6种情况:

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www'. [NC]
RewriteCond %{HTTP_HOST} ^(?:www'.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

Cases: 
C1: http://example.com/
C2: http://www.example.com/
C3: https://www.example.com/
C4: http://example.com/LineDemo/
C5: http://www.example.com/LineDemo/
C6: https://www.example.com/LineDemo/