Apache 2.4 + Codeigniter Urls 在 HTTP 上工作但不在 HTTPS 上工作

Apache 2.4 + Codeigniter Urls working on HTTP but not on HTTPS

我有一个 codeigniter 应用程序,其中包含使用 ssl 保护的动态通配符子域。 我的问题是只有主页链接可以使用 https 即

https://sub1.site.com -- 有效

https://sub1.site.com/register -- 给出 404 错误,指出在端口 443 上找不到文件

然而 sub1.site.com/register -- 有效

我已使用此片段配置我的网站。

这是我的 config.php 动态子域

   if (isset($_SERVER['HTTP_HOST']))
 22 {
 23   $protocol = ($_SERVER['SERVER_PORT'] == 443 ? 'https://' : 'http://');
 24   $config['base_url'] = $protocol.$_SERVER['HTTP_HOST'];
 25   $config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
 26 }
 27 else
 28 {
 29    $config['base_url'] = '';
 30 }

然后我也用它在 apache2.conf

中启用 htaccess
     AccessFileName .htaccess
154 
155 <Directory /var/www/>
156         Options  FollowSymLinks
157         AllowOverride All
158         Require all granted
159 </Directory>

最后这是我的 htaccess 文件

<IfModule mod_rewrite.c>
  2     RewriteEngine On
  3     RewriteBase /
  4     #this is to allow no indexes
  5     Options All +Indexes
  6         #www is not a subdomain 
  7         #RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
  8         #RewriteRule ^(.*)$ http://%1/ [R=301,L]
  9 
 10     #Removes access to the system folder by users.
 11     #Additionally this will allow you to create a System.php controller,
 12     #previously this would not have been possible.
 13     #'system' can be replaced if you have renamed your system folder.
 14     RewriteCond %{REQUEST_URI} ^system.*
 15     RewriteRule ^(.*)$ /index.php?/ [L]
 16 
 17     #When your application folder isn't in the system folder
 18     #This snippet prevents user access to the application folder
 19     #Submitted by: Fabdrol
 20     #Rename 'application' to your applications folder name.
 21     RewriteCond %{REQUEST_URI} ^application.*
 22     RewriteRule ^(.*)$ /index.php?/ [L]
 23 
 24     #Checks to see if the user is attempting to access a valid file,
 25     #such as an image or css document, if this isn't true it sends the
 26     #request to index.php
 27     RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
 28     RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
 29     RewriteRule ^(.*)$ index.php?/ [L]
 30 
 31 </IfModule>
 32 
 33 
 34 <IfModule !mod_rewrite.c>
 35     # If we don't have mod_rewrite installed, all 404's
 36     # can be sent to index.php, and everything works as normal.
 37     # Submitted by: ElliotHaughin
 38 
 39     ErrorDocument 404 /index.php
 40 </IfModule>
 41 AddType image/x-windows-bmp bmp

我在/etc/apache2/sites-available的虚拟主机配置如下

1 NameVirtualHost *:80
  2 <VirtualHost x.x.x.x:80>
  3         SSLEngine On
  4         SSLCertificateFile /etc/ssl/wild/STAR_site_mobi.crt
  5         SSLCertificateKeyFile /etc/ssl/wild/myserver.key
  6         SSLCertificateChainFile /etc/ssl/wild/site.ca-bundle
  7         
  8         ServerAdmin webmaster@localhost
  9         DocumentRoot /var/www
 10         ServerName www.site.mobi
 11         ServerAlias site.mobi
 12         Redirect / https://www.site.mobi
 13         
 14         <Directory />
 15                 Options FollowSymLinks
 16                 AllowOverride ALL
 17         </Directory>
 18         <Directory /var/www/>
 19                 Options +Indexes +FollowSymLinks +MultiViews
 20                 AllowOverride ALL
 21                 Require all granted
 22         </Directory>
 23         ErrorLog ${APACHE_LOG_DIR}/error.log
 24 
 25         # Possible values include: debug, info, notice, warn, error, crit,
 26         # alert, emerg.
 27         LogLevel warn
 28         
 29         CustomLog ${APACHE_LOG_DIR}/access.log combined
 30 
 31 </VirtualHost>
 32 <VirtualHost *:80>
 33         #SSLEngine On
 34         #SSLCertificateFile /etc/ssl/wild/STAR_site_mobi.crt
 35         #SSLCertificateKeyFile /etc/ssl/wild/myserver.key
 36         #SSLCertificateChainFile /etc/ssl/wild/STAR_site_mobi.ca-bundle
 37         
 38         ServerAdmin webmaster@localhost
 39         DocumentRoot /var/www
 40         ServerName site.mobi
 41         ServerAlias *.site.mobi
 42         Redirect / https://*.site.mobi
 43         <Directory />
 44                 AllowOverride ALL
 45         </Directory>
 46 </VirtualHost>
 47 <VirtualHost x.x.x.x:443>
48         #SSLEngine On
 49         #SSLCertificateFile /etc/ssl/wild/STAR_site_mobi.crt
 50         #SSLCertificateKeyFile /etc/ssl/wild/myserver.key
 51         #SSLCertificateChainFile /etc/ssl/wild/STAR_site_mobi.ca-bundle
 52 
 53         ServerAdmin webmaster@localhost
 54         DocumentRoot /var/www
 55         ServerName site.mobi
 56         ServerAlias *.site.mobi
 57         <Directory />
 58                 Options FollowSymLinks
 59                 AllowOverride ALL
 60         </Directory>
 61         <Directory /var/www/>
 62                 Options +Indexes +FollowSymLinks +Multiviews
 63                 AllowOverride ALL
 64                 Require all granted
 65 
 66         </Directory>
 67 
 68 </VirtualHost>

虚拟主机对我来说看起来不太好。 ssl 引擎应该在:443 而不是在每一页上即时阅读 .htaccess 规则,将它们包含在 /var/www/ 目录中一次 试试这样:

<VirtualHost *:80>
       DocumentRoot /var/www
       ServerName www.site.mobi
       ServerAlias site.mobi
       Redirect permanent / https://site.mobi
</VirtualHost>

<VirtualHost x.x.x.x:443>
       #ServerAdmin webmaster@localhost
       DocumentRoot /var/www
       ServerName site.mobi
       ServerAlias *.site.mobi
       <Directory />
               Options FollowSymLinks
               AllowOverride ALL
       </Directory>
       <Directory /var/www/>
           Order allow,deny
           Allow from all
           Require all granted
           Options +FollowSymLinks -Indexes
           RewriteEngine On

           # Instead of reading .htaccess rules on the fly every page,
           # Include them once, now, in this directory context
           AllowOverride None


          #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?/ [L]

          #When your application folder isn't in the system folder
          #This snippet prevents user access to the application folder
          #Submitted by: Fabdrol
          #Rename 'application' to your applications folder name.
          RewriteCond %{REQUEST_URI} ^application.*
          RewriteRule ^(.*)$ /index.php?/ [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?/ [L]
       </Directory>
       SSLEngine On
       SSLCertificateFile /etc/ssl/wild/STAR_site_mobi.crt
       SSLCertificateKeyFile /etc/ssl/wild/myserver.key
       SSLCertificateChainFile /etc/ssl/wild/site.ca-bundle
</VirtualHost>