URL redirection-rewrite using part of the URI

pinla11pinla11 Member

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

  • john_allenjohn_allen Member, A10ers ✭✭

    "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.

  • nehakakarnehakakar Member

    It seems there are a couple of issues on your script.

    1. The if condition is incorrect. You are using starts_with to match the HTTP::host, which may not work as expected. Instead, you can use a simple equality check for the host.
    2. The regexp command is not capturing the desired value properly.
    3. The HTTP::uri is being overwritten before using it in the HTTP::redirect command

    here 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.

Sign In or Register to comment.