简体   繁体   中英

Position of querystring parameters on friendly url

I dont know why this is not working in this htaccess rule: I want that friendly url can get 2 querystring params and it works fine, but the problem is the position that each param has in querystring.

I cant know the exact position of $2 and $3.

This is the url to try... http://myweb.com/ferrari-testarossa-car-id460?aa=red&bb=coupe

And this is my rule...

RewriteRule ^[A-Za-z0-9-\+\.]+-id([0-9]{1,5})[\?]?([a-z0-9\-\_]*[\=]?[a-z0-9]*[\.]?[a-z0-9]*)&([a-z0-9\-\_]*[\=]?[a-z0-9]*[\.]?[a-z0-9]*)$ car-profile.php?idModel=$1&$2&$3 [QSA,L]

I would need to pass key and value (both together) like this " aa=red " y " bb=coupe " to destination url as $2 and $3

How can i do this?

1000 thanks

With your shown samples, could you please try following. Please make sure to clear your browser cache before testing your URLs.

RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/.*id=(\d+)\?aa=([^&]*)&bb=(.*)\s [NC]
RewriteRule ^ car-profile.php?idModel=%1&%2&%3 [QSA,L]

finally.....I got it!

This is the correct regular expression:

RewriteRule ^[A-Za-z0-9-\+\.]+-id([0-9]{1,5})?$ car-profile.php?idModel=$1 [QSA,L]

When we put the flag " [QSA] ", entire querystring will be passed concatenated to $ 1 parameter, so, into your php page, you can get each value within typical $GET["param"]

Example: for this input url:

http://myweb.com/ferrari-testarossa-car-id460?bb=coupe&&aa=red

this will be got as output url:

http://myweb.com/car-profile.php?idModel=460?bb=coupe&&aa=red

It was easier than i thought.

Thnaks a lot for everyone

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