Search code examples
apache.htaccessmod-rewriteurl-rewriting

Can't do this Rewriterule


So I looked everywhere but I can't seem to make it work.

I have:

  • domain/pageX
  • domain/pageY
  • domain/pageZ etc

BUT if I have domain/CODE/page I need it to be domain/page?CODE Also if I have domain/CODE/page?p1&p2 I need it to be domain/page?p1&p2&CODE

CODE is a 2 character string which could be aa, bb or cc

Been trying stuff like:

RewriteRule ^aa/(.*?)$ $1&aa [NC,L,QSA]
RewriteRule ^aa/(.*)$ $1?aa [NC,L,QSA]

So I'm really lost on this topic, wasted many hours with no success, please enlighten me


Solution

  • I had been trying always these 2 ways, as in thinking when I have params and when I have no extra params

    RewriteRule ^aa/(.*?)$ $1&aa [NC,L,QSA]
    RewriteRule ^aa/(.*)$ $1?aa [NC,L,QSA]
    

    But I removed the first and it actually worked just with the second one. Extra parameters are passed automatically so I just get the added aa, bb or cc as another parameter. Just need to declare one rule for each of them

    RewriteRule ^aa/(.*)$ $1?aa [NC,L,QSA]
    RewriteRule ^bb/(.*)$ $1?bb [NC,L,QSA]
    RewriteRule ^cc/(.*)$ $1?cc [NC,L,QSA]
    

    Furthermore, if just aa is called without nothing extra, I added this rule for each:

    RewriteRule ^aa(.*)$ index?aa [NC,L,QSA]
    

    So it redirects to the index page and also passes the params + aa, browsing to aa/ goes through the first rule.

    Leaving my answer if anyone encounters this same scenario :D