Remove 'index.php' from URL with .htaccess

I've been trying all sorts of solutions from this site and none seem to work. I'm currently hosting with hostgator. This is my current .htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
<IfModule mod_suphp.c>
    suPHP_ConfigPath /home/user/php.ini
    <Files php.ini>
        order allow,deny
        deny from all

    </Files>
</IfModule>

This is in the root folder of my site. I have also tried adding a ? after index.php and no luck. Does anyone know why this isn't working?


Solution 1:

This is the code you can use in your .htaccess (under DOCUMENT_ROOT) to remove index.php from URI:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]

Solution 2:

Symfony 2 has an excellent solution:

RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]

# Sets the HTTP_AUTHORIZATION header removed by apache
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]


RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]

RewriteRule .? %{ENV:BASE}/index.php [L]

This accomplishes the following:

  • RewriteBase is not necessary (useful when the site is in a subdirectory beneath the web root)
  • index.php is removed if present
  • The request is routed to the correct index.php with the full query string from the original request

Note that the line:

RewriteRule ^(.*) - [E=BASE:%1]

is responsible for setting the %{ENV:BASE} variable for later on. Refer to Apache documentation on E|env flag.

Solution 3:

I tried this and works fine:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Removes index.php from ExpressionEngine URLs
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{REQUEST_URI} !/system/.* [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

    # Directs all EE web requests through the site index file
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

Exceptions

If your site’s system directory (/system/) has been renamed and is still accessible by URL, modify the RewriteCond line above:

RewriteCond %{REQUEST_URI} !/newdirectoryname/.* [NC]

If you are running EE from a sub-directory rather from the root of your domain (e.g. http://example.com/myeesite/ instead of http://example.com/), just remove the slash preceding index.php in the RewriteRule line above, like so:

RewriteRule ^(.*)$ index.php/$1 [L]

If you are running EE from a sub-directory and it still doesn’t work after removing the slash, you may need to specify the sub-directory in your rewrite rule. For example, if your sub-folder is named testing, change:

RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

To:

RewriteRule (.*?)index\.php/*(.*) testing/$1$2 [R=301,NE,L]

And change:

RewriteRule ^(.*)$ /index.php/$1 [L]

To:

RewriteRule ^(.*)$ testing/index.php/$1 [L]

If your host requires forcing query strings, try adding a question mark following index.php in the RewriteRule line above, like so:

RewriteRule ^(.*)$ /index.php?/$1 [L]

Solution 4:

To remove index.php from urls on apache2.4 you can use the following rule :

 RewriteEngine on

 RewriteRule ^index\.php/(.+)$ /$1 [L,R]
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule !index\.php /index.php%{REQUEST_URI} [L,END]

This will change the uri

/index.php/foobar

to

/foobar

This rule will return an internal server error for lower versions of apache as they don't support the END flag. Please see the anubhava's answer above that works almost on all versions.