Forum Discussion
Portion of iRule not being processed
I have combined multiple iRules into one, however the 2nd portion of the rule is not working. I have confirmed using logging that the URI is being caught, however redirection is not taking place. I believe I may not be nesting switch statements correctly.
I have commented which parts of the script are working, and which section is not. Can anyone point me in the right direction?
when HTTP_REQUEST {
These DO work
if { ([HTTP::host] equals "olddomain.com") } {
switch -glob [HTTP::host] {
"/" { HTTP::redirect "http://newdomain.com" }
default { HTTP::redirect "http://newdomain.com[HTTP::uri]" }
}
}
These do NOT work
switch -glob [string tolower [HTTP::host]] {
"oldforum.olddomain.com" {
Check the URI, set to lowercase
switch [string tolower [HTTP::query]] {
"TID=" {
HTTP::redirect "https://newforum.newdomain.com/default.aspx?g=posts&t=[URI::query [HTTP::uri] TID]"
return
}
"FID=" {
HTTP::redirect "https://newforum.newdomain.com/default.aspx?g=topics&f=[URI::query [HTTP::uri] FID]"
return
}
"C=" {
HTTP::redirect "https://newforum.newdomain.com/default.aspx?g=forum&c=[URI::query [HTTP::uri] C]"
return
}
}
}
These DO work
"*newdomain.com" - "*olddomain.com" {
switch -glob [string tolower [HTTP::uri]] {
"/someuri" {
HTTP::redirect "http://newdomain.com/path-to/page.aspx"
return
}
"/someuri2" {
HTTP::redirect "http://http://newdomain.com/path-to/page.aspx"
return
}
}
}
}
}
- Cory_50405Noctilucent
Your switch statement that isn't working is trying to match on three strings containing uppercase letters (TID=, FID=, and C=). You are first converting the match string to lower case though ([string tolower [HTTP::query]])
Change your match strings to tid=, fid=, and c= and it should work.
- Joe_PipitoneNimbostratus
Hmm.....nice catch!
Still doesn't seem to process.
That portion of the iRule works on its own, if I take it out of the nested switch and put each into an if statement....
- Cory_50405Noctilucent
Have you tried inserting a logging statement before the redirects within this switch statement to verify that the query is matching one of the strings? Or that the host is even matching oldforum.olddomain.com to get to evaluation of the nested switch?
when HTTP_REQUEST { switch [string tolower [HTTP::query]] { "tid=" { log local0. "Query tid= matched" HTTP::redirect "https://newforum.newdomain.com/default.aspx?g=posts&t=[URI::query [HTTP::uri] tid]" return } "fid=" { log local0. "Query fid= matched" HTTP::redirect "https://newforum.newdomain.com/default.aspx?g=topics&f=[URI::query [HTTP::uri] fid]" return } "c=" { log local0. "Query c= matched" HTTP::redirect "https://newforum.newdomain.com/default.aspx?g=forum&c=[URI::query [HTTP::uri] c]" return } } }
- Joe_PipitoneNimbostratus
Just tried that - it's not logging. The only way I had it logging before was to put log local0. "Query is [HTTP::query]" before the last curly brace. It knows what the query is, however it doesn't seem to be processing.
For instance - this will log the query:
These do NOT work switch -glob [string tolower [HTTP::host]] { "oldforum.olddomain.com" { Check the URI, set to lowercase switch [string tolower [HTTP::query]] { "tid=" { HTTP::redirect "https://newforum.newdomain.com/default.aspx?g=posts&t=[URI::query [HTTP::uri] tid]" return } "fid=" { HTTP::redirect "https://newforum.newdomain.com/default.aspx?g=topics&f=[URI::query [HTTP::uri] fid]" return } "c=" { HTTP::redirect "https://newforum.newdomain.com/default.aspx?g=forum&c=[URI::query [HTTP::uri] c]" return } } log local0. "Query is [HTTP::query]" }
- Cory_50405Noctilucent
In the example code you just posted, what gets logged as the query? Does it explicitly match one of the three strings you are matching on?
- Joe_PipitoneNimbostratusThis is what gets logged - only if I place the logging statement before the last curly brace: Rule test2 : Query is TID=52673&FID=25&title=thread-name
- Cory_50405Noctilucent
So the query string is more than just tid=..
Try changing your switch to this:
switch -glob [string tolower [HTTP::host]] { "oldforum.olddomain.com" { Check the URI, set to lowercase switch [string tolower [HTTP::query]] { "tid=*" { HTTP::redirect "https://newforum.newdomain.com/default.aspx?g=posts&t=[URI::query [HTTP::uri] tid]" return } "fid=*" { HTTP::redirect "https://newforum.newdomain.com/default.aspx?g=topics&f=[URI::query [HTTP::uri] fid]" return } "c=*" { HTTP::redirect "https://newforum.newdomain.com/default.aspx?g=forum&c=[URI::query [HTTP::uri] c]" return } } }
- Joe_PipitoneNimbostratusStill not working. Nothing logged either. It's been driving me crazy. I have several other rules with nested switch statements, haven't had a problem until now. I have isolated this portion of the rule by itself, still not logging or processing.
- Cory_50405Noctilucent
Just a thought, but try removing the = from the string to match against:
switch -glob [string tolower [HTTP::host]] { "oldforum.olddomain.com" { Check the URI, set to lowercase switch [string tolower [HTTP::query]] { "tid*" { HTTP::redirect "https://newforum.newdomain.com/default.aspx?g=posts&t=[URI::query [HTTP::uri] tid]" return } "fid*" { HTTP::redirect "https://newforum.newdomain.com/default.aspx?g=topics&f=[URI::query [HTTP::uri] fid]" return } "c*" { HTTP::redirect "https://newforum.newdomain.com/default.aspx?g=forum&c=[URI::query [HTTP::uri] c]" return } } }
- Joe_PipitoneNimbostratusStill no go. Nothing logged either. Only way I was able to get this to work was using if statements.
- Joe_PipitoneNimbostratus
This is what I'm testing with:
when HTTP_REQUEST { switch -glob [string tolower [HTTP::host]] { "oldforum.olddomain.com" { Check the URI, set to lowercase switch [string tolower [HTTP::query]] { "tid*" { HTTP::redirect "https://newforum.newdomain.com/default.aspx?g=posts&t=[URI::query [HTTP::uri] tid]" return } "fid*" { HTTP::redirect "https://newforum.newdomain.com/default.aspx?g=topics&f=[URI::query [HTTP::uri] fid]" return } "c*" { HTTP::redirect "https://newforum.newdomain.com/default.aspx?g=forum&c=[URI::query [HTTP::uri] c]" return } } } } }
- Cory_50405Noctilucent
Sorry, this one should finally work. Your nested switch needs the -glob:
when HTTP_REQUEST { switch -glob [string tolower [HTTP::host]] { "oldforum.olddomain.com" { Check the URI, set to lowercase switch -glob [string tolower [HTTP::query]] { "tid*" { HTTP::redirect "https://newforum.newdomain.com/default.aspx?g=posts&t=[URI::query [HTTP::uri] tid]" return } "fid*" { HTTP::redirect "https://newforum.newdomain.com/default.aspx?g=topics&f=[URI::query [HTTP::uri] fid]" return } "c*" { HTTP::redirect "https://newforum.newdomain.com/default.aspx?g=forum&c=[URI::query [HTTP::uri] c]" return } } } } }
- Joe_PipitoneNimbostratusGetting results now! Something strange is going on after the redirect takes place, but I will continue to troubleshoot. Thanks for your time - much appreciated.
- Cory_50405NoctilucentYou got it. Glad things are working for you now.
- Joe_PipitoneNimbostratus
I'm coming back to this after a few days - it appears as though the query is not being changed to lowercase, and doesn't process unless I manually change the TID, FID, or C to lower case. Then, the redirect happens properly.
Here is the iRule in its current state:
Check the Host header value, set to lowercase switch -glob [string tolower [HTTP::host]] { "oldforum.olddomain.com" { Check the URI, set to lowercase switch -glob [string tolower [HTTP::query]] { "tid=*" { log local0. "Query is [HTTP::query]" HTTP::redirect "http://newforum.newdomain.com/default.aspx?g=posts&t=[URI::query [HTTP::uri] tid]" return } "fid=*" { log local0. "Query is [HTTP::query]" HTTP::redirect "http://newforum.newdomain.com/default.aspx?g=topics&f=[URI::query [HTTP::uri] fid]" return } "c=*" { log local0. "Query is [HTTP::query]" HTTP::redirect "http://newforum.newdomain.com/default.aspx?g=forum&c=[URI::query [HTTP::uri] c]" return } } }
- Cory_50405NoctilucentThis iRule won't change the query string to lower case, it only compares a lower case version of the query string in order to match against. In your log statements, you should see the original query specified (with capital letters if that's how it was originally requested). Or are you saying that it's no longer even logging/getting into the individual match statements within the switch?
Recent Discussions
Related Content
* 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