Welcome Guest, Not a member yet? Register   Sign In
htaccess for removing index.php
#1

This is not a question - it's a tip.

There are many folks out there puzzled because the htaccess file supplied at the root of CI does not remove index.php from the URL. They've checked that mod_rewrite is on and that Allow overrides is on for their root, but still no joy. Lots of conflicting advice on Stackoverflow and the like. I tried it on three different Apache setups: at home it worked, at hosting provider A it worked, but at hosting provider B it did not.

Fortunately hosting provider B had a very smart support person who told me the fix.

The re-write rule supplied by CI is this : RewriteRule ^(.*)$ index.php/$1 [L]
The one that works on all Apache servers is this: RewriteRule ^(.*)$ index.php?/$1 [L]
Notice the ? at the end of index.php

It's backwards compatible - the new rule works on all the machines I tested it on.

So I'd like to suggest that the CI folks change the documentation for "Removing the index.php file"

Hope this will help someone

KT
Reply
#2

Can you tell under what condition htaccess content specified in documentation doesn't work? (Apache server version, configuration...)
Reply
#3

(This post was last modified: 04-10-2015, 03:42 AM by InsiteFX.)

Has to do with running Apache fast CGI

PHP Code:
<IfModule mod_rewrite.c>


    # Make sure directory listing is disabled
    
Options +FollowSymLinks -Indexes
    RewriteEngine on

    
# NOTICE: If you get a 404 play with combinations of the following commented out lines
    #AllowOverride All
    #RewriteBase /wherever/ci/is
    
RewriteBase /

    
# Restrict your site to only one domain
    # Important USE ONLY ONE OF THESE OPTIONS BELOW!

    # Option 1: To rewrite "www.domain.com -> domain.com" uncomment the following lines.
    #RewriteCond %{HTTPS} !=on
    #RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    #RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

    # Option 2: To rewrite "domain.com -> www.domain.com" uncomment the following lines.
    #RewriteCond %{HTTPS} !=on
    #RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
    #RewriteCond %{HTTP_HOST} (.+)$ [NC]
    #RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]

    # Option 3: Remove index.php from URL
    
RewriteCond %{HTTP:X-Requested-With}    !^XMLHttpRequest$
    
RewriteCond %{THE_REQUEST}                ^[^/]*/index\.php [NC]
    
RewriteRule ^index\.php(.*)$            $[R=301,NS,L]

    
# Send request via index.php (again, not if its a real file or folder)
    
RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond 
%{REQUEST_FILENAME} !-d

    RewriteCond 
$^(robots\.txt|favicon\.ico|style\.css)

    
# deal with php5-cgi first
    
<IfModule mod_fcgid.c>
        
RewriteRule ^(.*)$ index.php?/$[QSA,L]
    </
IfModule>

    <
IfModule !mod_fcgid.c>

        
# for normal Apache installations
        
<IfModule mod_php5.c>
            
RewriteRule ^(.*)$ index.php/$[QSA,L]
        </
IfModule>

        
# for Apache FCGI installations
        
<IfModule !mod_php5.c>
            
RewriteRule ^(.*)$ index.php?/$[QSA,L]
        </
IfModule>

    </
IfModule>

</
IfModule
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

thanks for the tip. was searching for the stuff a whole week. Wink
Reply
#5

(04-10-2015, 03:38 AM)InsiteFX Wrote: Has to do with running Apache fast CGI


PHP Code:
<IfModule mod_rewrite.c>


    # Make sure directory listing is disabled
    
Options +FollowSymLinks -Indexes
    RewriteEngine on

    
# NOTICE: If you get a 404 play with combinations of the following commented out lines
    #AllowOverride All
    #RewriteBase /wherever/ci/is
    
RewriteBase /

    
# Restrict your site to only one domain
    # Important USE ONLY ONE OF THESE OPTIONS BELOW!

    # Option 1: To rewrite "www.domain.com -> domain.com" uncomment the following lines.
    #RewriteCond %{HTTPS} !=on
    #RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    #RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

    # Option 2: To rewrite "domain.com -> www.domain.com" uncomment the following lines.
    #RewriteCond %{HTTPS} !=on
    #RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
    #RewriteCond %{HTTP_HOST} (.+)$ [NC]
    #RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]

    # Option 3: Remove index.php from URL
    
RewriteCond %{HTTP:X-Requested-With}    !^XMLHttpRequest$
    
RewriteCond %{THE_REQUEST}                ^[^/]*/index\.php [NC]
    
RewriteRule ^index\.php(.*)$            $[R=301,NS,L]

    
# Send request via index.php (again, not if its a real file or folder)
    
RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond 
%{REQUEST_FILENAME} !-d

    RewriteCond 
$^(robots\.txt|favicon\.ico|style\.css)

    
# deal with php5-cgi first
    
<IfModule mod_fcgid.c>
        
RewriteRule ^(.*)$ index.php?/$[QSA,L]
    </
IfModule>

    <
IfModule !mod_fcgid.c>

        
# for normal Apache installations
        
<IfModule mod_php5.c>
            
RewriteRule ^(.*)$ index.php/$[QSA,L]
        </
IfModule>

        
# for Apache FCGI installations
        
<IfModule !mod_php5.c>
            
RewriteRule ^(.*)$ index.php?/$[QSA,L]
        </
IfModule>

    </
IfModule>

</
IfModule

Yep - that's what the support guy told me. I don't think I would have figured that out on my own ! And thanks for the more detailed reply - I'm sure that will be useful for many folks
KT
Reply




Theme © iAndrew 2016 - Forum software by © MyBB