HTTPS和删除CodeIgniter上的index.php

时间:2016-08-15 08:54:18

标签: php apache .htaccess codeigniter mod-rewrite

我正在使用codeigniter框架,设置.htaccess文件以删除index.php并尝试为服务器启用HTTPS协议,并发生了一些事情。

HTTP:

  1. 当我访问http://www.example.com/controller/methodhttp://www.example.com/index.php/controller/method
  2. 时,一切正常

    HTTPS:

    1. 当我访问https://www.example.com/ index.php / controller / method

    2. 时,一切正常
    3. 访问https://www.example.com/controller/method

    4. 时找不到404

      我认为这是.htaccess文件问题,看起来htaccess文件不能用于HTTPS协议。

      我网站的.htaccess文件。

      DirectoryIndex index.php
      RewriteEngine on
      
      RewriteCond %{HTTPS} off
      RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
      
      RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|js|robots\.txt|favicon\.ico)
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]
      

      有什么不对吗?非常感谢。

5 个答案:

答案 0 :(得分:4)

注意:SSL重写应在index.php文件中.htaccess重写之前进行。

所以,不要这样做(因为我们不更改服务器apache配置文件中的任何内容。 )

  

AllowOverride All

只需应用设置2 即可。

我遇到了同样的问题并通过添加

解决了这个问题
  

AllowOverride All

设置1: 仅供参考,我们有2个配置文件。

  1. 默认Apache配置(使用http运行)
  2. SSL配置(使用https运行)
  3. 转到SSL配置并添加

    <Directory "/var/www/html">
       AllowOverride All
    </Directory> 
    

    **设置2:** .htaccess文件应该是这样的......

    RewriteEngine On
    RewriteCond %{HTTPS} !=on      
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^ index.php [L]
    </IfModule> 
    

    它对我有用。

答案 1 :(得分:0)

首先从配置文件中删除$ config ['base_url]并在$ config ['index']属性中添加''(空白)..

请将您的.htaccess文件设置为..

<IfModule mod_rewrite.c>
 RewriteEngine On

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d

  # Rewrite all other URLs to index.php/URL
  RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
  </IfModule>
  <IfModule !mod_rewrite.c>
      ErrorDocument 404 index.php
  </IfModule>

答案 2 :(得分:0)

试试这个

我在我的一个项目中使用了这个。愿它帮助你

#AuthType Basic
#AuthName "restricted area"
#AuthUserFile /home/site_name/public_html/.htpasswd
#require valid-user

RewriteEngine On
#RewriteBase /

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.site_name.com/$1 [R,L]

RewriteCond %{HTTP_HOST} ^([a-z.]+)?.site_name.lk$ [NC]
RewriteRule ^(.*)$ https://www.site_name.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^([a-z.]+)?site_name\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? https://www.%1site_name.com%{REQUEST_URI} [R=301,L]

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
#   RewriteCond $1 !^(index\.php|assets|files|robots\.txt)
#   RewriteRule ^(.*)$ /index.php/$1 [L]

    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
  
    上述htaccess中的

注意 site_name应替换为您的网站名称

  

答案 3 :(得分:0)

你可以尝试这个,它适用于我的项目。

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

参考:https://wiki.apache.org/httpd/RewriteHTTPToHTTPS

答案 4 :(得分:0)

如果您要http-> https并删除www并删除index.php,请使用以下解决方案:

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ index.php [L]
</IfModule>