Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 635
Norse Avatar asked Mar 07 '12 20:03

Norse


People also ask

How do I disable index php in WordPress?

To remove index. php from your WordPress URL, login to your WordPress Dashboard, go the Settings > click on permalinks, and there change the permalink structure to Post name. Now check your website URL, the index. php be gone.


2 Answers

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]
like image 172
anubhava Avatar answered Oct 29 '22 19:10

anubhava


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.

like image 25
Frédéric Klee Avatar answered Oct 29 '22 20:10

Frédéric Klee