Skip to content
Advertisement

Get id (category or business) from url after mod rewrite and assign into variable in php

The actual URL’s are like – /category.php?id=2 and /product.php?id=56 . I applied the following rules in my htaccess file –

RewriteEngine On
RewriteRule   ^.+/p/([0-9]+)   product.php?id=$1    [NC,L]
RewriteRule   ^.+/c/([0-9]+)   category.php?id=$1    [NC,L]

And i am getting the following url patterns –

/c/2 and `/p/56` 

Upto this it is fine Previously i could get the ID’s of category and product from like ?id= 2 by GET method. But get method will not work after rewrite. How can i get the ID’s also the page type (category,products)?

Advertisement

Answer

Try this

RewriteEngine On
RewriteRule   ^.+/p/([0-9]+)   product.php?id=$1    [QSA,L]
RewriteRule   ^.+/c/([0-9]+)   category.php?id=$1    [QSA,L]

QSA means that if there’s a query string passed with the original URL, it will be appended to the rewrite.

L means if the rule matches, don’t process any more RewriteRules below this one.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement