Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 example.com/path/index.php, it should turn into: example.com/path/

But if the url is example.com/path/index.php?blah=0, 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
add comment

1 Answer

up vote 2 down vote accepted

Use

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: archstl.org/education/… It is rewritten as: archstl.org/… 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
add comment

Your Answer

 
discard

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.