apache - 强制 Codeigniter HMVC 使用 SSL

标签 apache codeigniter ssl

我有一个使用 Codeigniter HMVC 构建的网站。在我使用 Wamp 的本地机器上,它运行正常。我想将它上传到我的具有 SSL 证书的网络服务器。

无论我尝试什么,我都无法让网站在我的网络服务器上运行。我试过这样做: How to force ssl in codeigniter? 但是没有一个给定的解决方案适用于我的情况。

我的 .htaccess 文件如下所示:

Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

在 codeigniter 的 config.php 中我有:

$config['base_url'] = 'https://www.example.com/';

我的网络服务器是一台装有 apache 的 ubuntu 16 机器,使用 letsencrypt 证书。 mod rewrite 已启用,但除此之外是标准配置。

我已尝试使用上面链接中提到的钩子(Hook)进行重定向,但无济于事。我也尝试过各种 .htaccess 文件配置,但也没有结果。

有人在他们的服务器上运行这个吗?

问候,

砂光机

最佳答案

因为你说,

my webserver is an ubuntu 16 machine

我假设您没有使用来自托管提供商的共享服务器,因此您可以完全访问 Apache 的配置文件。如果是这样,那么您不应使用 .htaccess 文件。阅读THISTHIS了解原因。

我还假设您有一组定义 <VirtualHost> 的 Apache“可用站点”文件配置。它在里面 <VirtualHost> .htaccess 中应该放置当前代码的 block 。

要将所有 http 请求重定向到 https,请尝试将其作为“可用站点”配置文件中“默认”站点的内容,例如“000-default.conf”

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    RedirectMatch 301 (.*) https://www.example.com$1
</VirtualHost>

这将执行任何 http 请求并立即重定向到 https: URL。如果您使用的是典型 :80 以外的其他端口,请相应地进行调整。

如前所述,您可以在 VirtualHost 中进行重写。这是 https: 配置文件的示例(可能按照“020-example-443.conf”行命名)

<VirtualHost *:443>
    ServerName www.example.com
    ServerAlias example.com
    DocumentRoot /var/www/whatever
    ServerAdmin web-boss@example.com

    <Directory /var/www/whatever>
        DirectoryIndex index.php
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted

        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php?/$1 [PT]
    </Directory>

    #   SSL Engine Switch:
    #   Enable/Disable SSL for this virtual host.
    SSLEngine on

    # Certificates delivered by certbot - your's maybe elsewhere
    SSLCertificateFile  /etc/letsencrypt/live/yoursite/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/yoursite/privkey.pem

    #SSLOptions +FakeBasicAuth +ExportCertData +StrictRequire
    <FilesMatch "\.(cgi|shtml|phtml|php)$">
    SSLOptions +StdEnvVars
    </FilesMatch>

    <Directory /usr/lib/cgi-bin>
        SSLOptions +StdEnvVars
    </Directory>
</VirtualHost>

有了这些,您就不必在 CodeIgniter 中做任何特殊的事情,除了像您已经做的那样,使用以下内容

$config['base_url'] = 'https://www.example.com/';

关于apache - 强制 Codeigniter HMVC 使用 SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53209767/

相关文章:

java - 如何使用ant添加jar操作

php - 使用 codeigniter 将字符串日期时间插入到 mysql 数据库中

php - 如何从 3 个表中获取数据?功能

apache - Apache httpd 背后的 Tomcat 通过 SSL(httpd 工作,tomcat 返回错误 500)

java - 如何查找 Java 中使用的 SSL/TLS 版本

java - PDFBox 2.0.8 签署文档时出现问题

python - 如何在错误 apache 服务器容器上修复 500 内部服务器

java - 使用 AJP 连接器和 mod_proxy 在 Tomcat 前面的 Apache 出错

php - Codeigniter 图像处理问题

ssl - 当内容位于另一个域时,如何在一个域上使用 SSL 证书?