Technical Forum
Ask questions. Discover Answers.
cancel
Showing results for 
Search instead for 
Did you mean: 
Custom Alert Banner

Capture part of URL to redirect to another

Village_Idiot
Nimbostratus
Nimbostratus

I have a request to redirect from one URL to another based on the word "LOAN" in the query part of the URL. They only want to keep a random length string from the old URL to the redirected one

Example

From

https://website1.domain.com/path/app.aspx?KT1112_0_0_0=38932940&KY108_0_0=LOAN&clienttype=html&cqid=...

To

https://website2.domain.com/path/app.aspx?acctNum=38932940&acctType=LOAN&type=Documents

In the examples, the only part needed to capture is "38932940" and use it as a variable in the redirected URL. The problem is this is random in length. Any ideas on how to achieve this?

1 ACCEPTED SOLUTION

CA_Valli
MVP
MVP

Hello there,
if the requested information is always stored in variable KT1112_0_0_0= at request time, this script will capture exact variable value (not bound to length) and insert it in redirect url as acctNum value

when HTTP_REQUEST {
  if { [HTTP::uri] contains "LOAN" }{
    set acctNum [findstr [HTTP::query] "KT1112_0_0_0=" 13 &]
    HTTP::redirect "https://website2.domain.com/path/app.aspx?acctNum=$acctNum&acctType=LOAN&type=Documents"
	}
}

It's working in my lab

CA_Valli_0-1668674730897.png

View solution in original post

4 REPLIES 4

Hi @Village_Idiot , 
 > I have written an iRule script for you and tested it in my home Lab environment. 

> First take the iRule : 

when HTTP_REQUEST {
  if { [HTTP::uri] contains "LOAN"} {
  HTTP::redirect "https://website2.domain.com/path/app.aspx?acctNum=[URI::query https://website1.domain.com/path/app.aspx?KT1112_0_0_0=38932940&KY108_0_0=LOAN&clienttype=html&cqid=102 KT1112_0_0_0]&acctType=LOAN&type=Documents"
  }
}

#URI::query <uri> <parameter>    I used it to Get the numeric value from a specific parameter exists in first uri

it should work with you as its as long you do not have any new parameters you want to add , it’s up to you. 
> Read about [URI::query] Function which was my clue to get the desired output. 

___________________________________________________________________________
Now , I will share a snap shots from my environment : 

  1- My irule which is adequate with my environment and published application : 
my irule.PNG

2- URL before Redirection and fetching the parameter value , it "q" Parameter in my environment : 

before Redirection and transferring Parameter value.PNG

3- URL after Redirection and transfer the "q" value to be stored in parameter "acctNum": 
After Redirection..PNG

as it is shown "acctNum" parameter has taken the "q" parameter value in the first request and Redirected to the second URL with the targgeted value. 

hope it helps you. 
Please test it and give me your feedback to save it in my notes if it matches your requirements as it is the first time to use [url::query] Function. 

Regards
Mohamed Kansoh

_______________________
Regards
Mohamed Kansoh

CA_Valli
MVP
MVP

Hello there,
if the requested information is always stored in variable KT1112_0_0_0= at request time, this script will capture exact variable value (not bound to length) and insert it in redirect url as acctNum value

when HTTP_REQUEST {
  if { [HTTP::uri] contains "LOAN" }{
    set acctNum [findstr [HTTP::query] "KT1112_0_0_0=" 13 &]
    HTTP::redirect "https://website2.domain.com/path/app.aspx?acctNum=$acctNum&acctType=LOAN&type=Documents"
	}
}

It's working in my lab

CA_Valli_0-1668674730897.png

This worked, but I had to make an adjustment to the findstr as it wasn't working. Here is the final iRule

when HTTP_REQUEST {
  if { [HTTP::uri] contains "LOAN" }{
    set acctNum [findstr [HTTP::query] "=" 1 &]
    HTTP::redirect "https://website2.domain.com/path/app.aspx?acctNum=$acctNum&acctType=LOAN&type=Documents"
	}
}

Thanks for the help!!

I was capturing the full query parameter and skipping the first 13 characters (param name + "=" character) in order just to keep the value. 

Be careful with you syntax, because if another parameter is passed in querystring before acctNum value (like that LOAN that triggers the if condition) it might be captured instead. If param name is dynamic (eg. numbers change), you could also capture a "static" part and skip the dynamic pattern using an appropirate syntax. 

[findstr [HTTP::query] "KT1112" 13 &] will match all of the following param names, and save the param value after the "=" and up to first "&" character:  

KT1112_0_0_0=

KT1112_1_2_3=

KT1112aaaaaa=

I really think you should configure the match to be as close as possible. 

Regards

CA