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

时间:2014-04-04 02:52:22

标签: apache .htaccess mod-rewrite

与摔跤有一段时间没有成功。我正在寻找的是确保所有请求的.htaccess规则:

  1. 始终以" www"
  2. 开头
  3. 始终是安全的(https)
  4. 在我尝试过的所有规则中,也许这是最接近的规则:

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

    这套规则适用于:

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

    不适用于:

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

    更新:

    在我的情况下,实际问题是,对于某些文件扩展名(.cfm,.cfml,.cfc),请求正在被切换"在我的重写规则能够生效之前到应用程序服务器。感谢所有回复的人。

2 个答案:

答案 0 :(得分:1)

尝试此规则:

RewriteEngine On

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

答案 1 :(得分:0)

这应该做的工作:

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

请注意,.htaccess应位于网站主文件夹中。