【问题标题】:rewrite htaccess to https with route使用路由将 htaccess 重写为 https
【发布时间】:2014-12-11 21:56:53
【问题描述】:

我正在为我的网站使用以下 .htaccess。如果它们不是位于 url 的文件,我会将所有 url 重定向到 index.php。

Options +FollowSymLinks
IndexIgnore */*
# Turn on the RewriteEngine
RewriteEngine On
#  Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

这样http://website.com/user/user_name/ 会转到 index.php 和 http://website.com/css/style.css 给出了实际的 css 文件(如果 style.css 存在的话)。

这很好用,只是我想在 url 上强制 https:// 并且我无法让它工作。任何帮助是极大的赞赏。

编辑:

使用 Jeroen 的回答,我得到了它与以下 .htaccess 的配合

Options +FollowSymLinks
IndexIgnore */*
# Turn on the RewriteEngine
RewriteEngine On
#  Rules
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

【问题讨论】:

    标签: php .htaccess mod-rewrite https


    【解决方案1】:

    试试这个

    <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteCond %{HTTPS} off
      RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    </IfModule>
    

    【讨论】:

    • 它将我发送到 https,但它给了我一个重定向循环。
    • @KoenHendriks 你在用 CloudFlare 的 ssl 吗?
    【解决方案2】:

    当您使用 CloudFlare 的 ssl - 我假设灵活 SSL - 您需要不同的解决方案,尽管 @Fox 的解决方案对于“正常”ssl 连接是正确的。

    对于 CloudFlare,您需要以下内容:

    RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
    RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L,QSA]
    

    另见CloudFlare's support article

    【讨论】:

    • 添加了这个并且它有效。谢谢!你能解释一下为什么 cloudflare 需要这种不同的处理方式吗?
    • @KoenHendriks 您没有真正的 ssl 连接到您的服务器,只有从客户端到 CloudFlare。从 CloudFlare 到您的服务器的连接是正常的 http 连接,因此您的原始解决方案不起作用。但是,CloudFlare 添加了一个 http 标头,以便您可以检查访问者是否请求了 http 或 https 连接,这就是您在我的答案中看到的 CloudFlare CF-Visitor 标头。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-15
    • 2017-12-04
    • 2014-03-04
    • 2021-12-28
    • 1970-01-01
    相关资源
    最近更新 更多