Forum Discussion
Leslie_South_55
Nimbostratus
Oct 19, 2007findclass AND external class AND wildcards
I am using an external class file to do URI redirects, when using speciffic URI matching it works fine:
when HTTP_REQUEST {
log local0. "Uri is [HTTP::uri]"
set NewURI [findclass [HTTP::uri] $::wwwredirects " "]
log local0. "NewURI is $NewURI"
if { $NewURI ne ""}{
HTTP::redirect "http://[HTTP::host]$NewURI"
log local0. "redirecting to http://[HTTP::host]$NewURI"
}
}
and my wwwredirects would look something like this
"/accounts/orders/ /application/query/accounts/orders"
but now I have 2 challenges to enhance this:
1) I need to be able to include wildcards (/accounts/orders/* --> /newuri/blah)
2) I need to be able to re-write the HTTP::host if needed (/accounts/newaccount/* --> https://www.host.com/register/signup.htm)
Can I do all of this (static redirects, wildcard redirects, and complete HOST and URI redirects) from one rule and one class file?
Thanks
-L
- Leslie_South_55
Nimbostratus
Bump :-) - hoolio
Cirrostratus
I don't think you can use wildcards with findclass. One option would be to loop through the class using 'foreach line $::class' and do a string comparison using 'starts_with' or 'string match' against the element of the line.... foreach line $::class { if {[HTTP::path] starts_with [getfield $line " " 1]}{ HTTP::redirect "http://[HTTP::host][getfield $line " " 2]" } }
- Leslie_South_55
Nimbostratus
I do not undestand where the $line and the 1 and 2 come it. Are these varibles that I define or is it how I format the class file? - Leslie_South_55
Nimbostratus
I modified your sample to look like thiswhen HTTP_REQUEST {
Oct 22 15:23:57 tmm tmm[25714]: Rule rule_wwwredirecttest : URI is /accounts/orders
- hoolio
Cirrostratus
Sorry, I should have included more comments...foreach one_line $::class_or_array { do something }
class string_class { "111 aaa" "222 bbb" "333 ccc" }
when RULE_INIT { foreach line $::string_class { log local0. "One Line: \$line: $line" log local0. "\[getfield \$line \" \" 1\]: [getfield $line " " 1]" log local0. "\[getfield \$line \" \" 2\]: [getfield $line " " 2]" } }
One Line: $line: 111 aaa [getfield $line " " 1]: 111 [getfield $line " " 2]: aaa One Line: $line: 222 bbb [getfield $line " " 1]: 222 [getfield $line " " 2]: bbb One Line: $line: 333 ccc [getfield $line " " 1]: 333 [getfield $line " " 2]: ccc
when HTTP_REQUEST { log local0. "URI is [HTTP::uri]" foreach line $::wwwredirects { log local0. "Current line from the class: $line" if {[HTTP::path] starts_with [getfield $line " " 1]}{ log local0. "Matched: [HTTP::path] to: [getfield $line " " 1]" HTTP::redirect "http://[HTTP::host][getfield $line " " 2]" log local0. "redirecting to http://[HTTP::host][getfield $line " " 2]" Break from the foreach loop once a match is found so no further checks/redirects are done break } } }
- Leslie_South_55
Nimbostratus
I seem to be having an issue with the class. The file keeps getting un-formatted. I build the file like this: - hoolio
Cirrostratus
Can you try formatting the file with just one pair of double quotes per line? - hoolio
Cirrostratus
One other thing I realized: return will exit the rule. break would be better as it only exits the foreach loop.
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