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

时间:2015-01-02 06:31:24

标签: php apache .htaccess codeigniter

我有一个带有动态通配符子域的codeigniter应用程序,使用ssl进行保护。 我的问题是只有家庭链接使用https即

https://sub1.site.com - 作品

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

然而 sub1.site.com/register - 工作

我已使用此代码段来配置我的网站。

这是动态子域名的配置文件

   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/$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?/$1 [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?/$1 [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?/$1 [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>

1 个答案:

答案 0 :(得分:0)

vhost对我来说不太好看。 ssl引擎应该在:443 而不是每页都在读取.htaccess规则,而是将它们包含在/ var / www / Directory中 尝试像这样:...

<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?/$1 [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?/$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]
       </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>