0

http:// domain.com/index.php -> http:// domain.com/ - OK

http:// domain.com/index.php?z=abc -> http:// domain.com/index.php?z=abc - not OK, stays the same. This works using the code below:

RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]

How to modify this code so that

http:// domain.com/index.php?z=abc -> http:// domain.com/?z=abc
Haradzieniec
  • 9,086
  • 31
  • 117
  • 212

2 Answers2

1

Apache will take care of the query string automatically. All you need is this:

RewriteEngine on
RewriteRule ^index.php / [L,R=301]
kba
  • 19,333
  • 5
  • 62
  • 89
  • The [R=301] will break POST requests (form data lost), but leaving it out will not affect the address bar. For this reason, you would want to avoid doing this kind of rewrite if (ever) POSTing to index. – Umbrella Jan 11 '12 at 21:49
  • @Umbrella Most servers default to 301, anyway. And since he doesn't want `/index.php` to be shown in the address bar, I doubt he'll be posting to it. – kba Jan 11 '12 at 22:25
  • @KristianAntonsen A reasonable doubt, but since the OP opened a new question about this breaking his POSTs, I figured the reminder might be helpful to someone else finding this thread. – Umbrella Jan 11 '12 at 23:01
1

Try the modified RewriteCond below

RewriteEngine On

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php(\?[^\ ]*)?\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]
Ulrich Palha
  • 9,411
  • 3
  • 25
  • 31