2
RewriteEngine On
RewriteRule ^detail.html?id=([0-9]+)-page=([0-9]+)$ detail.php?id=$1&page=$2

I rewrite URL detail.php?id=1&page=1 to detail.html?id=1&page=1. I always get the 404 errors.

3
  • Just to clarify, your code is rewriting from detail.html to detail.php, but your description that follows states the opposite?
    – MrWhite
    Dec 26 '15 at 21:02
  • detail.php to detail.html Dec 27 '15 at 3:28
  • Erm, are you sure? Internally rewriting from .php to .html is not "normal". Or is this intended to be an external redirect? Which URL do you want to see in the browser's address bar - the one the user sees?
    – MrWhite
    Dec 27 '15 at 10:18
1

You can't match the query string using RewriteRule. The RewriteRule pattern matches against the URL-path only, excluding the query string. You would need to use a RewriteCond directive and check against %{QUERY_STRING}. (So your directive never matches and you get a 404.)

However, it doesn't look like you need to match the query as you are simply rewriting the URL's "file" extension from .html to .php. Which could be done with something like:

RewriteRule ^detail\.html$ detail.php [L]

The query string is automatically copied to the destination URL (providing you don't explicitly specify one in the substitution).

You should also escape the dot (eg. \.) in the RewriteRule pattern (which is a regular expression), otherwise it will match any character.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.