user3312792
user3312792

Reputation: 1111

rewrite 2 php variables clean url

I have website.co.uk/product.php?id=123456&name=name-of-product

and would like to rewrite this to:

website.co.uk/product/123456/name-of-product

or even better:

website.co.uk/123456/name-of-product

I have this which rewrites the first variable $id

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

to product/123456

just need the second variable

Upvotes: 1

Views: 492

Answers (2)

bbb
bbb

Reputation: 702

Simply match another variable (correct the range to suit your needs):

RewriteRule    ^product/([0-9]+)/([a-zA-Z0-9-]+)/?$    product.php?id=$1&name=$2    [NC,L]    

When in doubt working with regular expressions, I highly recommend The Regex Coach, great small software for experimenting and testing your expressions.

Upvotes: 0

anubhava
anubhava

Reputation: 784958

You can add another rule for the 2 parameter URI:

RewriteRule ^product/([0-9]+)/?$ product.php?id=$1 [NC,L,QSA]

RewriteRule ^product/([0-9]+)/([\w-]+)/?$ product.php?id=$1&name=$2 [NC,L,QSA]

Upvotes: 2

Related Questions