【问题标题】:Redirecting to HTTPS returns error "too many redirects"重定向到 HTTPS 返回错误“重定向过多”
【发布时间】:2015-12-11 20:32:16
【问题描述】:

我正在尝试通过 htaccess 强制 https 并且收到“重定向过多”错误。以下是我用来尝试完成此任务的方法。

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

关于我如何实现这一点或为什么这似乎不起作用的任何想法?

编辑:我按照这里的答案Force https://www. for Codeigniter in htaccess with mod_rewrite 这似乎导致了我的重定向循环。当我删除这部分时:

RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

重定向循环消失;但是它不会重定向到 https。

【问题讨论】:

标签: .htaccess redirect


【解决方案1】:

我不能确定这是否与(或某些)托管服务提供商有关,但没有一个解决方案对我有用。由于托管服务提供商的限制,我无法获得 apache 版本,除非它是 2.x。

这就是这样做的,也许对某人有帮助:

RewriteEngine On
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

【讨论】:

    【解决方案2】:

    正如 RiggsFolly 所说,该语句存在问题,但如果这不起作用,您可以尝试在 head 标签中强制它:

    $use_sts = true;
    
    // iis sets HTTPS to 'off' for non-SSL requests
    if ($use_sts && isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
        header('Strict-Transport-Security: max-age=31536000');
    } elseif ($use_sts) {
        header('Location: https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'], true, 301);
        // we are in cleartext at the moment, prevent further execution and output
        die();
    }``
    

    【讨论】:

    • 如果我不能让它在 htaccess 中工作,我会试一试。谢谢。
    猜你喜欢
    • 2017-04-14
    • 2021-08-04
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 2017-06-14
    • 2018-12-15
    相关资源
    最近更新 更多