skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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


  2. 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]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search