Home > Blockchain >  Can't do this Rewriterule
Can't do this Rewriterule

Time:12-06

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

CodePudding user response:

With your shown samples and attempts please try following .htaccess rules. This considers that you have to rewrite traffic to index.php in backend if this is not the case then change file name to as per your requirement please.

RewriteEngine ON

##External redirect to page?CODE page. 
RewriteCond %{THE_REQUEST} \s/([^/]*)/(page)\s [NC]
RewriteRule ^ /%2?%1 [R=301,L]

##Internal rewrite to page?CODE page. 
RewriteCond %{THE_REQUEST} \s/page\?(\S )\s
RewriteRule ^  index.php?queryString=%1 [QSA,NC,L]

##External redirect to page?p1&p2&CODE.
RewriteCond %{THE_REQUEST} \s/([^/]*)/(page)\?([^&]*)&(\S )\s [NC]
RewriteRule ^ /%2?%3&%4&%1 [R=301,L]

##Internal rewrite to page?p1&p2&CODE.
RewriteCond %{THE_REQUEST} \s/page\?([^&]*)&([^&]*)&(\S )\s
RewriteRule ^  index.php?%1&%2&%3 [QSA,NC,L]

NOTE: Change index.php?queryString in rule to as per your file name and query string which you want to pass.

CodePudding user response:

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

  • Related