Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I can't quite figure out how to use a regex in my .htaccess file to remove index.php from the end of a URL if and only if it is the last thing in the string.

If the url is, it should turn into:

But if the url is, it should stay the same.

Any help here? I thought this might work, but it doesn't seem to preserve the latter case above:

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

share|improve this question
up vote 2 down vote accepted


RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*/)index.php$ $1 [L]
share|improve this answer
I tried it - it does remove index.php, but it still removes it if there are more arguments after the PHP in the URL... For instance, if the URL is:… It is rewritten as:… For this URL, I'd like to keep the /index.php in the link... – geerlingguy Feb 22 '10 at 13:58
So whenever there are GET parameters in the URL, you want to keep it as is ? Just trying to make sure I understand your requirements ... – Dominik Feb 22 '10 at 15:35
Yes, exactly (sorry it wasn't more clear) – geerlingguy Feb 22 '10 at 15:46
I've updated my answer to include a rewritecond checking that the query_string is empty – Dominik Feb 22 '10 at 16:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.