1

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]

geerlingguy
  • 1,357
  • 2
  • 17
  • 29

1 Answers1

2

Use

RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*/)index.php$ $1 [L]
Dominik
  • 2,218
  • 14
  • 9
  • 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: http://archstl.org/education/index.php?option=com_content&task=view&id=167&Itemid=258 It is rewritten as: http://archstl.org/education?option=com_content&task=view&id=167&Itemid=258 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