Question

I want to redirect this:

example.com/product-name-p12.html (it's product_page.php?id=12 rewrited)

to:

www.example.com/product-name-p12.html

If I'm using this Rewrite rule:

RewriteRule ^(.*)-p(.*).html$ product_page.php?id=$2&%{QUERY_STRING}

RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

it's redirecting to www.example.com/product_page.php?id=12, not to the html version.

Thanks a lot,

Was it helpful?

Solution

Reverse the ordering of your rule and use QSA flag. QSA (Query String Append) flag preserves existing query parameters while adding a new one.

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NE]

RewriteRule ^(.*)-p(.*)\.html$ product_page.php?id=$2 [L,QSA]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top