HTTPS and Remove index.php on CodeIgniter

ε祈祈猫儿з 提交于 2019-12-12 03:21:46

问题


I am using codeigniter framework,set .htaccess file to remove index.php and trying to enable HTTPS protocol for the server, and something happened.

HTTP:

  1. Everything is okay, when I access http://www.example.com/controller/method or http://www.example.com/index.php/controller/method

HTTPS:

  1. Everything is okay, when I access https://www.example.com/index.php/controller/method

  2. Got 404 not found when I access https://www.example.com/controller/method

I think that is .htaccess file problem, it is look like htaccess file not working for HTTPS protocol.

.htaccess file of my site.

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]

Is something wrong? thanks a lot.


回答1:


Note: The SSL rewrite should happen before the index.php rewrite in your .htaccess file.

So, Don't do this setting (as we don't change anything in server apache config file. )

AllowOverride All

just apply the Setting 2 it works.


I have faced the same issue and resolved it by adding

AllowOverride All

Setting 1: FYI we have 2 config files.

  1. Default Apache config (which runs with http)
  2. SSL config (which runs with https)

Go to SSL config and add

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

**Setting 2:**The .htaccess file should be like this...

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> 

It worked for me.




回答2:


First you remove $config['base_url] from your config file and put ''(blank) in your $config['index'] attribute ..

please set your .htaccess file like..

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



回答3:


Try this

I used this in one of my project. May its helps you

#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>

Note site_name in the above htaccess should replaced to your website name




回答4:


you can try this, it works for my project.

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]

reference: https://wiki.apache.org/httpd/RewriteHTTPToHTTPS




回答5:


if you want to http->https AND remove www AND remove index.php here's the solution:

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> 


来源:https://stackoverflow.com/questions/38952095/https-and-remove-index-php-on-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!