2011-12-14 78 views
12

我用笨并按照these instructions强制SSL,但所有的请求都被重定向到强制https:// www。对于笨与mod_rewrite的

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

htaccess的我.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] 
+0

酷的故事!问题是什么? SO!= debug-it-for-me.com – 2011-12-14 11:31:29

回答

16

我想,而不是

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

你应该有类似

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

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

18

你可以在代码中使用而不是使用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(); 
     ... 
    } 
} 
+2

从我的角度来看,协议是服务器应该处理的东西,如果可能的话(应用程序需求),应用程序不需要任何东西。客户可以使用HTTP或HTTPS,或者甚至可以使用[SPDY](https://en.wikipedia.org/wiki/SPDY)。 – feeela 2012-11-14 20:51:36

+0

我认为这是CI的一个非常优雅的解决方案。虽然我可以理解feeela的观点,但我发现从应用程序内控制它与CI工作流和风格是一致的。同样,其他常用的.htaccess任务也是通过CI的路由功能来处理的。 – 2014-02-21 16:08:06

7

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] 
0

想必你有RewriteEngine On上面这段代码的某个地方......

RewriteCond %{REQUEST_URI} ^system.* 

可能不会被触发。 REQUEST_URI应该先从/(不同于重写规则),所以你可能想

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 ..

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); 
      } 
     } 
    } 
} 

希望它有帮助。