php - 使用 mod_rewrite 在 htaccess 中为 Codeigniter 强制使用 https ://www.

标签 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]

最佳答案

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

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

在你的助手中;

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

然后在你的 Controller 中;

class Whatever extends CI_Controller {

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

    public function index() {
        force_ssl();
        ...
    }
}

关于php - 使用 mod_rewrite 在 htaccess 中为 Codeigniter 强制使用 https ://www.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8503663/

相关文章:

php - 在 yii2 gridview 中输入时进行过滤

php - 修剪空格后在 jQuery 数据表中进行内联编辑验证

php - 匹配数量和单位的正则表达式

apache - 在不使用 .htaccess 的子文件夹上强制使用 https

php - 无法在 .htaccess 中为我的 php 网站编写重写规则

wordpress - 如果我使用 Wordpress 而不是 php 框架来处理复杂但 "standard"的 webapp,我会失去什么?权衡是否值得?

mysql - Codeigniter: $this->load->database();返回空白页

php - 将 block 内的 block 与 pcre 匹配

php - 如何在 WordPress 中为带有斜杠的页面创建自定义 url,如/se/pagename

java - 从字符串中获取指定单词 php codeigniter