简体   繁体   English

.htaccess添加www并确保https://

[英].htaccess add www AND ensure https://

Been wrestling with this for quite a while with no success. 摔跤已经很长时间了,没有成功。 What I'm looking for is an .htaccess rule to ensure all requests: 我正在寻找的是.htaccess规则以确保所有请求:

  1. always start with "www" 总是以“ www”开头
  2. are always secure (https) 始终安全(https)

Of all the rules I tried, perhaps this was the closest: 在我尝试过的所有规则中,也许这是最接近的:

RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301]

This set of rules works great for: 这套规则非常适用于:

  • http://www.example.com > https://www.example.com http://www.example.com > https://www.example.com
  • http://example.com > https://www.example.com http://example.com > https://www.example.com

Doesn't work for: 不适用于:

  • http://www.example.com/index.cfm?myquerystring
  • http://example.com/index.cfm?myquerystring

UPDATE: 更新:

The actual problem in my case is that for certain file extensions (.cfm,.cfml,.cfc) the request was being "handed off" to the application server before my rewrite rules were able to take affect. 在我的情况下,实际的问题是,对于某些文件扩展名(.cfm,.cfml,.cfc),在我的重写规则生效之前,该请求已“移交给”应用程序服务器。 Thanks to all who responded. 感谢所有回应。

Try this rule: 尝试以下规则:

RewriteEngine On

RewriteCond %{SERVER_PORT} 80 [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]

This should do the job: 这应该做的工作:

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

Please, note that the .htaccess should be located in the website main folder. 请注意,.htaccess文件应位于网站主文件夹中。

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

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