Forum Discussion
Bibits_102724
Nimbostratus
Sep 09, 2010Need help with a redirect iRule
Hello,
I am new to iRules, and having problems getting it to work the way I hoped it would. What I want to solve is to redirect URLs according to the three examples below.
example 1:
http://my.website.no/database1/openurl.ashx?aulast=ludlum ==>
http://my.website.no/test/web/openurl.ashx?db=database1&unit=6465&aulast=ludlum
example 2:
http://my.website.no/database1 ==>
http://my.website.no/test/web/default.aspx?db=database1&unit=6465
example 3:
http://my.website.no/test/web/default.aspx?db=database1&unit=6465 ==>
http://my.website.no/test/web/default.aspx?db=database1&unit=6465
I also have a redirect file I figured would be on the format:
"/database1 db=database1&unit=6465",
Most requests will be on the format of example 3, where the request should pass untouched.
I was picturing an iRule structured somehing like the one below. But I can't even get a value into $left, so I must be doing something wrong. I am pretty sure that the 'split' and 'set left' commands worked yesterday, but when I try them now it doesn't work at all. I was also wondering if the reason for my problem is because split gives me a list, but I believe that when I fetch one value with "set left [lindex $params 0]" it should give me a string.
The way I think of doing this is to try to split an URI into two parts, the left ($left) and right ($right) side of "openurl.ashx?". I want to do this because if there is no "openurl.ashx" in the URI then I figure everything will end upp in $left
If there is no value in $right then I know that the URI is either on the format of example 2 or example 3. And if there is no value in $location I know that the URI is on the format of of example 3.
I am hoping that I can use the findclass method the way I am trying, but it depends of wether or not $left is really a string. I am also wondering if this is a reasonable way of structuring the iRule (if it ends up working at all), or if there is a more effective way,
Best regards,
Morten
----------------------------
Here is the draft for the iRule I am picturing:
when HTTP_REQUEST {
set params [split [HTTP::uri] "/openurl.ashx?"]
left holds the value for testing the file
set left [lindex $params 0]
right holds the appended query (if it exists)
set right [lindex $params 1]
find part of redirect location from file based on $left
set location [findclass $left $::redirect_file " "]
check if we have a match in the file
if { $location ne "" }{
check if we have an appended query
if { $right ne "" }{
we have a match in the file, and a query to append
HTTP::redirect "http://my.website.no/test/web/openurl.ashx?$location$right"
return
}
we only have a basic redirect and no query to append
HTTP::redirect "http://my.website.no/test/web/default.aspx?$location"
}
If no match so we do nothing
}
2 Replies
- The_Bhattman
Nimbostratus
Hi Morten,
There are several commands referenced in the deventral wiki Click Here that can be used to make your draft more efficient as well as cut down the amount code lines in the process.
Here is quick untested example that might start you down path that you are looking forwhen HTTP_REQUEST { switch -glob [URI::path [HTTP::uri] 1 ] { Match anything that starts with /database1 "/database1/*" { set location [findclass [URI::path [HTTP::uri] 1 1] $::redirect_file " "] HTTP::redirection "http://[HTTP::host]/test/web/[URI::basename]?$location[URI::query]" } Exact match "/database1" { set location [findclass [URI::path [HTTP::uri] 1 1] $::redirect_file " "] HTTP::redirection "http://[HTTP::host]/test/web/default.aspx?$location" } If not match then do nothing } }
As you can see using the following commands "switch, HTTP::uri, HTTP::host, URI::query, URI::basename and URI::path" in effect replaced the IF statement, several set statements, and the lindex command.
I hope this helps
Bhattman - Bibits_102724
Nimbostratus
Hello Bhattman,
Thank you for you help, much appreciated.
A switch statement seems like a good idea, I was actually wondering how I could get it to work. The reason I though of using a split and set statement is because I am thinking of "database1" as a variable, and what I must match in the redirect file. But I see now that I could think of "database1" (which can be "database2, etc) as a wildcard, and match other parts of the path.
I will have to work some more to test for eventualities, but I think some variation of the code below will work for me.
when HTTP_REQUEST {
switch -glob [string tolower [HTTP::path]] {
"/test/web*" {
break do nothing, most requests match here
}
"*openurl.ashx*" {
set name [URI::path [HTTP::uri] 1 1]
set name [string range $name 0 end-1] chop the trailing back slash
set location [findclass $name $::redirect_file " "]
HTTP::redirect "http://[HTTP::host]/test/web/openurl.ashx?$location[URI::query [HTTP::uri]]"
}
default {
set location [findclass [HTTP::uri] $::redirect_file " "]
HTTP::redirect /test/web/default.aspx?$location
}
}
}
Pity it takes ages to test changes in our BigIP. The GUI there must be the slowest GUI ever invented -- some 20-30 seconds to navigate to a new page there or to make a change.
Thanks again for pointing me in the right direction.
Best regards,
Morten
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects