如何强制HTTPS(Cloudflare Flexible SSL)?

时间:2016-01-18 23:34:07

标签: php apache .htaccess ssl https

我在自己编程的网站上使用Cloudflare Flexible SSL(没有框架或CMS)。一切正常,现在我想在整个网站上使用HTTPS。我在Apache Web服务器上使用PHP。

我想知道如何处理此问题并将所有用户重定向到HTTPS。

目前我的.htaccess设置如下:

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

我在stackoverflow上看到了this answer,但它指向的another answer并不简单doesn't recommend rewrites

这是Apache的建议:

<VirtualHost *:80>
    ServerName www.example.com
    Redirect "/" "https://www.example.com/"
</VirtualHost >

<VirtualHost *:443>
    ServerName www.example.com
    # ... SSL configuration goes here
</VirtualHost >

但坦率地说,我不知道这意味着什么。

如何将用户重定向到HTTPS和www?切换到HTTPS时我应该注意什么?

4 个答案:

答案 0 :(得分:3)

最好的方法是使用Cloudflare。

在Cloudflare网站上:

  1. 转到“页面规则”顶部按钮
  2. 添加新规则:始终使用https [ON]
  3. 或者您可以在.htaccess

    中使用
    RewriteEngine On
    # Redirect with www
    RewriteCond %{HTTP_HOST} !^www\.example\.com [NC,OR]
    # Redirect to https
    #    With Cloudflare:
    RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
    #    Without Cloudflare:
    # RewriteCond %{HTTPS} off 
    RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,R=301,L]
    

答案 1 :(得分:1)

好吧,我认为有一种简便的方法
刚刚打开云弹dashbaoard
转到加密部分
在SSL部分中,选择完整 向下滚动,您将看到此部分

始终使用HTTPS 将所有使用方案“ HTTP”的请求重定向到“ HTTPS”。这适用于对该区域的所有HTTP请求。
开启
全部完成

Source

答案 2 :(得分:0)

这样的事可能有用:

   RewriteEngine On
   RewriteCond %{HTTPS} off
   RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

答案 3 :(得分:0)

您可以尝试将其放入.htaccess文件中。

RewriteEngine On
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^(.*)$ https://www.example.com$1 [L]    

这将检查访问者是否正在访问http。如果是这样,它将重定向到同一页面的https版本。