views:

538

answers:

5

I'd like all queries like

http://mysite.com/something/otherthing?foo=bar&x=y

to be rewritten as

http://mysite.com/something/otherthing.php?foo=bar&x=y

In other words, just make the .php extension optional, universally.

A: 

Something like...

RewriteRule /something/(.+)?(.+) /something/$1.php?$2

would probably work.

theraccoonbear
That doesn't seem to work.
dreeves
You probably need to escape the question mark with a backslash
John Sheehan
I tried that. I think the query string stuff is just not part of what is matched in a RewriteRule. You have to do a separate RewriteCond on the query string. See my answer.
dreeves
+1  A: 

This works:

RewriteCond %{QUERY_STRING} ^.+$
RewriteRule ^/?([^/\.]+)$ /$1.php [L]

The idea is to make sure there's a query string (question mark plus stuff) and if so check if the stuff before the question mark has no extension and if so, append .php.

dreeves
+1  A: 

If you can change the httpd.conf and what to, you can also put:

ForceType application/x-httpd-php

in the file as it will force all the paths called to be PHP files. I think this also works with query strings.

Darryl Hein
+1  A: 

Matches only paths with no extension:

RewriteRule ^(([^/]+/+)*[^\.]+)$ $1.php

Edit: In my testing, the query string gets passed on automatically. If it doesn't, you can use this instead:

RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(([^/]+/+)*[^\.]+)$ $1.php?%1
eyelidlessness
+3  A: 

I would do it this way. Basically, if file doesn't exist, try adding .php to it.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ $1.php [QSA,L]
Chris Bartow
This is what I'm doing now. Perfect. Thanks!
dreeves