简体   繁体   中英

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

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]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM