Nelfo
Nelfo

Reputation: 3835

Modify this rewrite to ignore .php extension in query string?

This rewrite rule works just fine to remove .php extension, but in the case where a file name is passed in the query string, it tries to remove the extension there too. How to make it ignore the query string?

RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]

Upvotes: 0

Views: 221

Answers (1)

Jon Lin
Jon Lin

Reputation: 143886

You need to make the (.*) match in the rewrite condition more strict. Right now, it's matching the URI or the query string. Try:

RewriteCond %{THE_REQUEST} ^GET\ ([^\?]+)\.php
RewriteRule (.*)\.php$ $1 [R=301]

The [^\?]+ will match anything that isn't a ?. Removing the \ HTTP ensures that a query string could follow the .php.

Upvotes: 1

Related Questions