URL redirection-rewrite using part of the URI
Hello,
I been working on an aflex to do redirection of a web site.
Here's what I' trying to achieve:
Src host: mySRCsite.com
Src path: /services/?app=test&annee=2022
Dst host: myDSTsite.com
Dst path: /test/app1/USER=toi&LANGUE=FM&AN_ATTRB=2022
Redirection works, problem is that is hard coded at this time. What I want is check "annee" parameter on src path, save it on a variable and use this variable to overwrite AN_ATTRB value... that way I dont have to remember every year, that I need to change the aflex to correspond the right dates.
This is the aflex I wrote, but it doesn't work
if {([string tolower [HTTP::host]] starts_with "mySRCsite.com") and ([HTTP::uri] contains "/services/?app=test&annee=")} {
set uri [HTTP::uri]
set path ""
regexp -nocase {(\/services\/?app=test&annee=)(.*)} $uri match path
HTTP::uri /test/app1/USER=toi&LANGUE=FM&AN_ATTRB=$path
HTTP::redirect https://myDSTsite.com[HTTP::uri]
}
Can anyone help me about this? 😞
Thanks in advance!
Comments
"but it doesn't work" .... so what is not working? The IF statement? the regexp? Are you getting an error?
If it's the regexp, I think you need to escape your '?', as that is a valid regex quantifier.
It seems there are a couple of issues on your script.
if
condition is incorrect. You are usingstarts_with
to match the HTTP::host, which may not work as expected. Instead, you can use a simple equality check for the host.regexp
command is not capturing the desired value properly.HTTP::uri
is being overwritten before using it in theHTTP::redirect
commandhere is the correct version...
if {([HTTP::host] eq "mySRCsite.com") and ([HTTP::uri] contains "/services/?app=test&annee=")} {
set uri [HTTP::uri]
set path ""
regexp -nocase {/services/?app=test&annee=(.*)} $uri match path
HTTP::redirect "https://myDSTsite.com/test/app1/USER=toi&LANGUE=FM&AN_ATTRB=$path"
}
Try this and do let me know if this work to you.