【问题标题】:HTTPS and Remove index.php on CodeIgniterHTTPS 和删除 CodeIgniter 上的 index.php
【发布时间】:2016-08-15 08:54:17
【问题描述】:

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

HTTP:

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

HTTPS:

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

  2. 访问https://www.example.com/controller/method时找不到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]

有什么问题吗?非常感谢。

【问题讨论】:

  • 只需检查您是否确保文件名正确,其中文件名和类的第一个字母是大写的。
  • @wolfgang1983 文件名正确,通过 HTTP 协议访问网站时一切正常,但通过 HTTPS 协议访问网站时不起作用,看起来 .htaccess 文件仅适用于 HTTPS 协议。

标签: php apache .htaccess codeigniter mod-rewrite


【解决方案1】:

注意:SSL 重写应在 .htaccess 文件中的 index.php 重写之前发生。

所以,不要进行此设置(因为我们不会更改服务器 apache 配置文件中的任何内容。 )

允许全部覆盖

只需应用 设置 2 即可。


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

允许全部覆盖

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

  1. 默认 Apache 配置(使用 http 运行)
  2. SSL 配置(使用 https 运行)

转到 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> 

它对我有用。

【讨论】:

  • 你的答案是绝对正确的,它也适用于我。谢谢
  • yasshh...谢谢..它可以工作..并且需要在配置后重新启动apache2
【解决方案2】:

首先你从你的配置文件中删除 $config['base_url] 并把 ''(blank) 放在你的 $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>

【讨论】:

  • 对不起,它仅适用于 http,仍然不适用于 https 协议。
【解决方案3】:

试试这个

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

#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 应替换为您的网站名称

【讨论】:

    【解决方案4】:

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

    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

    【讨论】:

      【解决方案5】:

      如果你想 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> 
      

      【讨论】:

        猜你喜欢
        • 2016-05-17
        • 1970-01-01
        • 1970-01-01
        • 2012-09-30
        • 2016-07-05
        • 1970-01-01
        • 2011-07-09
        • 1970-01-01
        • 2012-10-30
        相关资源
        最近更新 更多