2

If user access this URL(http://domain.com/products/view-product.php?productID=21) in browser, it should show the page http://domain.com/new/view-product.php?pID=21

I have tried bellow code but it wont working, so please help me to resolve.

RewriteCond %{REQUEST_FILENAME} -f 
RewriteCond %{THE_REQUEST} products/view-product.php 
RewriteRule ^products/view-product.php?productID=21$ new/view-product.php?pID=21 [R=301,L]

products/view-product.php are physically exists in the server, still i want to rewrite it to the new page that i have designed now.

rkaartikeyan
  • 1,977
  • 9
  • 29
  • 57
  • possible duplicate of [URL Rewrite GET parameters](http://stackoverflow.com/questions/18162686/url-rewrite-get-parameters) – umka May 26 '15 at 20:40
  • @umka No its not duplicating... the old url and directory and files are exists, still i want to rewrite it to new url. – rkaartikeyan May 26 '15 at 20:42
  • have a look in the website, it will help you understand more about RewriteRule `https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/` – alexandreferris May 26 '15 at 20:43

3 Answers3

3

You can't match against the query string (everything after the ?) in a rewrite rule. Only the URI is used to match against that regular expression. You need to use the condition:

RewriteCond %{REQUEST_FILENAME} -f 
RewriteCond %{THE_REQUEST} products/view-product.php\?productID=21($|\ |&)
RewriteRule ^products/view-product.php$ /new/view-product.php?pID=21 [R=301,L]
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • thanks for your quick reply... its not working... products/view-product.php are physically existing in server. – rkaartikeyan May 26 '15 at 20:47
  • @rkaartikeyan doesn't matter if it exists or not. The condition says it has to exist for the rule to work. It also has to be **the first rule**. The rewrite engine processes a request before it is mapped to a file, this all happens regardless if that file exist or not. – Jon Lin May 26 '15 at 21:00
  • :( i dont have a luck :S – rkaartikeyan May 26 '15 at 21:05
  • i have found one small bug... i fixed and its works :) thanks a lot. – rkaartikeyan May 26 '15 at 21:35
2

You can match query string using RewriteCond and %{QUERY_STRING} variable. Query string params didn't get to RewriteRule.

RewriteEngine on
RewriteCond %{QUERY_STRING} ^productID=21$
RewriteRule ^products/view-product\.php$ /new/view-product.php?pID=21 [R=301,L]

Or if you want to redirect all productID's:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^productID=([0-9]+)$
RewriteRule ^products/view-product\.php$ /new/view-product.php?pID=%1 [R=301,L]
umka
  • 1,655
  • 1
  • 12
  • 18
0

GET the value of productID and then redirect to the new URL.

Rahul Kumar
  • 99
  • 3
  • 10