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

Ben*_*Ben 13 php .htaccess mod-rewrite codeigniter

我正在使用Codeigniter并按照这些说明强制ssl,但所有请求都被重定向到

http://staging.example.com/index.php/https:/staging.example.com
Run Code Online (Sandbox Code Playgroud)

.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]
Run Code Online (Sandbox Code Playgroud)

Roo*_*eyl 19

你可以在代码中而不是使用htaccess.

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

在你的助手;

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

然后在你的控制器;

class Whatever extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper(array('your_ssl_helper'));
    }

    public function index() {
        force_ssl();
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 从我的角度来看,协议是服务器应该处理的东西,如果可能的话,应用程序没有任何东西(尊重应用程序要求).客户端可以使用HTTP或HTTPS,甚至可以使用[SPDY](https://en.wikipedia.org/wiki/SPDY). (2认同)

fee*_*ela 16

我认为,而不是

RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Run Code Online (Sandbox Code Playgroud)

你应该有类似的东西

RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Run Code Online (Sandbox Code Playgroud)

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


Mon*_*ffy 8

用这个

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

代替

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Run Code Online (Sandbox Code Playgroud)