通过htaccess强制使用非www和https

问题描述

| 我正在尝试强制将用户重定向到非www网站,并强制使用https。 我已经完成了这项工作,但是当输入http时,不会强制使用https。
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://site.com\\.net/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\\.
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
关于我在做什么错的任何想法吗?     

解决方法

        尝试以下规则:
RewriteCond %{HTTP_HOST} ^(www\\.)(.+) [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI} [R=301,L]
    ,        根据Gumbo的评论:\“建立TLS / SSL连接并验证证书,然后将其传递给HTTP,然后进行HTTP重定向\” 我尝试了一下(似乎可行):
RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.blahblah.com/$1 [R,L]

RewriteCond %{HTTP_HOST} ^www\\.blahblah\\.com [NC]
RewriteRule ^(.*)$ https://blahblah.com/$1 [L,R=301]
请告诉我这种方法是否有问题。     ,        唯一适用于我的规则如下
# match any URL with www and rewrite it to https without the www
    RewriteCond %{HTTP_HOST} ^(www\\.)(.*) [NC]
    RewriteRule (.*) https://%2%{REQUEST_URI} [R=301,L]

# match non https and redirect to https
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
顺序很重要,在某些情况下,它阻止了第三次重定向。