1
votes

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.

5
Just check also have you made sure you have named your files correct where the first letter only of filename and class is upper case.Mr. ED
@wolfgang1983 filename is correct and everything is work when access website via HTTP protocol, but it is not work when access website via HTTPS protocol, it is look like .htaccess file not working for HTTPS protocol only.Gener Kill

5 Answers

7
votes

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.

0
votes

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

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

0
votes

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

0
votes

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>