0

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]
3
  • No, it does not. Please do your homework, and check on an actual system before asking. Commented Dec 9, 2012 at 20:57
  • Thank you for your comment Gerben, but perhaps you can add substance to it? The rule has been tested and does work. Perhaps you misunderstood the question? Commented Dec 9, 2012 at 23:50
  • Sorry. The RewriteCond will indeed match a querystring, but this is where the RewriteRule comes in. The RewriteRule only matches the path-part of the url, and never contains the querystring. So the (.*)\.php$ will never match if .php is only in the querystring. So the two combined should never, ever, match a url with only .php in the querystring. Commented Dec 10, 2012 at 7:39

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.