hiding index.php using htaccess- looping redirect


Posted by admin | Posted in Blog | Posted on 10-05-2009

A client recently had a problem with a website that they had moved from one server to another.. both were cpanel hosted servers and looked to be similar configuration. The issue he faced was that the .htaccess file he used before now caused infinite loops when going to the main page. The .htaccess file was being used to try and hide the index.php filename from the URL. The reason this was done was to make a more friendly URL and to stop anyone realising that there was a global index.php in the root that checked authentication etc..

The effect was that the URL http://www.domainname.com/index.php/applicationpage could be represented as http://www.domainname.com/applicationpage

The loop did not really make sense, my initial thoughts were towards permissions, but that path quickly lead no-where. Doing a Google search for “.htaccess file causing infinite loop” returned a huge number of results, most of which did not apply to what we were trying to achieve. Eventually I got more specific with the search and started searching for results that also included server configuration issues/solutions. Eventually I stumbled across this page:


Which explained that some servers required an extra question mark in their .htaccess RewriteRule… so that rather than reading..
RewriteRule ^(.*)$ /index.php/$1 [L]
we added a ? after the index.php to read
RewriteRule ^(.*)$ /index.php?/$1 [L]

Voila, the solution worked perfectly, ending hours of frustration for both me and the client 🙂

The ending .htaccess file read

RewriteEngine On
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

Comments are closed.