.htaccess - 在 codeigniter 中强制使用 SSL

标签 .htaccess codeigniter ssl

我已经在网上搜索并尝试了大多数事情,最近几个小时但没有结果。

我使用了 CodeIgniter,我有一个。 Htaccess 文件,其中我有一些规则。

我想强制 SSL 连接并从 url 中删除 index.php。

这是我的.Htaccess

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /

    # Force SSL
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

    #remove ugly index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin
    ErrorDocument 404 /index.php
</IfModule> 
Options All -Indexes

问题是它不起作用。我在 www.lotusmodellen.se 上安装了证书,所有流量都必须去那里。不是 lotusmodellen.se

请尝试进入这样的http://www.lotusmodellen.sehttp://lotusmodellen.se

无论你写什么,有或没有 https,你都应该来 www.lotusmodellen.se

不过好像是有问题,重定向错误什么的。因为它不起作用。

有没有人有解决办法?

最佳答案

您好,只需在 config.php 文件中替换您的 base_url,这将检查来自 https 或 http 的请求,将根据该请求设置您的 base_url。

if(isset($_SERVER['HTTP_HOST']))
{
    $config['base_url'] = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https' : 'http';
    $config['base_url'] .= '://'. $_SERVER['HTTP_HOST'];
    $config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
}

else
{
    $config['base_url'] = 'http://localhost/';
}

关于.htaccess - 在 codeigniter 中强制使用 SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11483312/

相关文章:

apache - .htaccess 未在 Ubuntu 14.04 (Apache 2.4.7) 中加载

.htaccess - 向浏览器发送预压缩/预压缩的 CSS 和 JS

php - 同一应用程序是否可以有多个 .htaccess 文件?

php - Codeigniter,如何获取以json格式显示的数据库表数据?

php - 使用 SimpleXML 读取 RSS 提要时出现问题

python - python中的SSL验证错误

.htaccess - 如何修复 ".htaccess RewriteRule results in duplicated pages"

CodeIgniter:将数据作为对象与数组返回

ruby-on-rails - 使用 Rails 4、NGinx 和 puma 获得无限重定向循环

ssl - Selenium Webdriver SSL 证书主机名不匹配问题