简体   繁体   中英

.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:

  1. always start with "www"
  2. are always secure (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://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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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