简体   繁体   English

无法通过htaccess强制使用HTTPS和非WWW

[英]Can't Force HTTPS and Non-WWW via htaccess

I am trying to force any regular http request to redirect to https, and also force non www in the URLs, so that every request in the end will be https://example.com 我正在尝试强制将任何常规http请求重定向到https,并在URL中也强制使用非www,以便最后的每个请求均为https://example.com

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

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

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

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

I have the following code in my htaccess file. 我的htaccess文件中包含以下代码。

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://example.com/$1 [R,L]

RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

But I am getting an error that states it has too many redirects. 但是我收到一个错误消息,指出它具有太多的重定向。 It seems to work fine on this test site with the correct output: 在此测试站点上,使用正确的输出似乎可以正常工作:

htaaccess tester preview htaac​​cess测试仪预览

Any ideas or a better approach to this? 有什么想法或更好的方法吗?

The following code should help you: 以下代码应为您提供帮助:

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

It will redirect everything to https://example.com 它将所有内容重定向到https://example.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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