【问题标题】:Force https://www. for Codeigniter in htaccess with mod_rewrite强制 https://www。使用 mod_rewrite 在 htaccess 中使用 Codeigniter
【发布时间】:2012-01-20 04:18:09
【问题描述】:

我正在使用 Codeigniter 并关注 these instructions 来强制使用 ssl,但所有请求都被重定向到

http://staging.example.com/index.php/https:/staging.example.com

我的.htaccess 是:

### Canonicalize codeigniter URLs

# Enforce SSL https://www. 
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

###
# Removes access to the system folder by users.
# Additionally this will allow you to create a System.php controller,
# previously this would not have been possible.
# 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]

# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

【问题讨论】:

  • 很酷的故事!问题是什么? SO != debug-it-for-me.com

标签: php .htaccess codeigniter mod-rewrite


【解决方案1】:

我为此编写了以下课程。我更喜欢在代码中包含这样的东西而不是配置文件,这样行为就不能被错误的配置修改(即页面真正“强制”使用 SSL)。

该函数必须在您网站加载过程的早期阶段调用。

class SecurityHelper extends MX_Controller
{
    // CONTROLLERS FORCED TO SSL
    private $arrHttps = array(
        'products/shipping',
        'products/billing'
    );

    // CONTROLLERS ACCESSIBLE FROM BOTH
    private $arrAgnostic = array (
        'products/getcart'
    );

    function redirectSsl()
    {
        // SSL MODULES
        if(in_array(uri_string(), $this->arrHttps))
        {
            // ONLY REDIRECT IF NECESSARY
            if ($_SERVER['HTTPS'] != "on")
            {
                // REDIRECTING TO SSL
                $newUrl = str_replace('http://', 'https://', base_url($_SERVER['REQUEST_URI']));
                redirect($newUrl);
            }
        }
        // NON-SSL MODULES
        else
        {
            // IF AGNOSTIC, DON'T REDIRECT
            if(in_array(uri_string(), $this->arrAgnostic))
                return;

            // ONLY REDIRECT IF NECESSARY
            if ($_SERVER['HTTPS'] == "on")
            {
                $newUrl = str_replace('https://', 'http://', base_url($_SERVER['REQUEST_URI']));
                redirect($newUrl);
            }
        }
    }
}

希望对你有帮助。

【讨论】:

    【解决方案2】:

    您可以在代码中完成,而不是使用 htaccess。

    您可以创建一个辅助函数,将页面重定向到您从控制器调用的 SSL。

    在你的助手中;

    function force_ssl() {
        if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on") {
            $url = "https://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
            redirect($url);
            exit;
        }
    }
    

    然后在你的控制器中;

    class Whatever extends CI_Controller {
    
        function __construct() {
            parent::__construct();
            $this->load->helper(array('your_ssl_helper'));
        }
    
        public function index() {
            force_ssl();
            ...
        }
    }
    

    【讨论】:

    • 从我的角度来看,如果可能的话,协议是服务器应该处理的东西,而不需要应用程序与它有任何关系(尊重应用程序要求)。客户端可以使用 HTTP 或 HTTPS,甚至可以使用 SPDY
    • 我认为这是一个非常优雅的 CI 解决方案。虽然我很欣赏feeela 的观点,但我发现从您的应用程序中控制这一点与CI 工作流程和风格是一致的。同样,其他常见的 .htaccess 任务也使用 CI 的路由功能处理。
    • 我需要将常量从 $_SERVER['HTTPS'] != "on" 更改为 $_SERVER['HTTPS'] != "1" 才能在我的系统中工作。跨度>
    【解决方案3】:

    大概你有RewriteEngine On这个代码上面的某个地方......

    RewriteCond %{REQUEST_URI} ^system.*
    

    可能不会被触发。 REQUEST_URI 应该以 / 开头(与 RewriteRule 不同),所以你可能想要

    RewriteCond %{REQUEST_URI} ^/system
    

    我不确定

    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

    将可靠地与 ^ 匹配。我会尝试以下方法:

    RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [L,R=301]
    

    顺便说一下,HTTP_HOST 通常是访问者输入的任何内容,因此 www.不能保证,如果那是你想要的。您将需要一个单独的步骤来强制 www..

    【讨论】:

      【解决方案4】:

      使用这个

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

      而不是

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

      【讨论】:

        【解决方案5】:

        我认为,而不是

        RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
        

        你应该有类似的东西

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

        确实有重写规则匹配。您的链接目前是由第三条规则生成的。

        【讨论】:

          猜你喜欢
          • 2017-01-08
          • 1970-01-01
          • 2013-05-31
          • 2012-08-22
          • 2011-05-22
          • 2018-04-22
          • 2011-01-25
          • 2014-01-20
          相关资源
          最近更新 更多