【问题标题】:forcing Codeigniter HMVC to use SSL强制 Codeigniter HMVC 使用 SSL
【发布时间】:2019-04-12 01:57:00
【问题描述】:

我有一个使用 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 已启用,但除此之外是标准配置。

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

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

问候,

砂光机

【问题讨论】:

  • 您正在使用 LetsEncrypt。您是自己生成证书还是使用 Certbot? certbot 的一项很酷的功能是您可以让它进行必要的更改以将所有 http/80 流量重定向到 https/443

标签: apache codeigniter ssl


【解决方案1】:

因为你说,

我的网络服务器是 ubuntu 16 机器

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

我还假设您有一组定义 <VirtualHost> 配置的 Apache“站点可用”文件。它位于那些 <VirtualHost> 块内,您应该将当前拥有的代码放置在 .htaccess 中。

要将所有 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/';

【讨论】:

    【解决方案2】:

    你必须在RewriteEngine on之后添加

    <IfModule mod_ssl.c>
        RewriteCond %{HTTPS} off
        RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    </IfModule>
    

    【讨论】:

      猜你喜欢
      • 2012-07-14
      • 2012-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-19
      • 2013-03-13
      • 1970-01-01
      相关资源
      最近更新 更多