强制https:// www。对于使用mod_rewrite的htaccess中的Codeigniter

时间:2011-12-14 11:22:33

标签: php .htaccess codeigniter mod-rewrite

我正在使用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]

5 个答案:

答案 0 :(得分:19)

您可以在代码中执行此操作,而不是使用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();
        ...
    }
}

答案 1 :(得分:16)

我认为,而不是

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

你应该有像

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

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

答案 2 :(得分:8)

使用此

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]

答案 3 :(得分:0)

据推测,您在此代码之上的位置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 :(得分:0)

我为此目的编写了以下课程。我更喜欢在配置文件中的代码中使用这样的东西,因此不能通过错误的配置修改行为(即页面真的被强制"到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);
            }
        }
    }
}

希望它有所帮助。

相关问题