Search code examples
regex.htaccessurlmod-rewritestructure

htaccess mod_rewrite expression for specific url structure


I have a URL-structure like this:

mypage.com/search/f7_987_Aprilia/f8_1234_Aprilia+RS+125
mypage.com/search/f7_987_Aprilia
mypage.com/search/f8_1234_Aprilia+RS+125

And I need them to route internally (without redirection) to the following structure:

mypage.com/search?&f[7]=987&f[8]=1234
mypage.com/search?&f[7]=987
mypage.com/search?&f[8]=1234

I have come this far: /(f\d+)(\d+).*/ This gives me the desired IDs in the first set. But how do I get the parameters separated by slashes? And how do I "convert" to the desired structure?

Is that possible with htaccess?

I hope there is a genius out there who can come up with the desired mod_rewrite regex

Thank you in advance!


Solution

  • You may use this code in site root .htaccess:

    Options -MultiViews
    RewriteEngine On
    
    RewriteRule ^search/([a-z])(\d)_(\d+)_[^/]*/?$ search?&$1[$2]=$3 [L,QSA,NC]
    
    RewriteRule ^search/([a-z])(\d)_(\d+)_[^/]*/([a-z])(\d)_(\d+)_[^/]*/([a-z])(\d)_(\d+)_[^/]*/?$ search?&$1[$2]=$3&$4[$5]=$6 [L,QSA,NC]