Forum Discussion
Spence_Faschin1
Nimbostratus
Mar 15, 2005Rewrite a uri (not redirect)
I have a need to rewrite a uri, and make this look transparent to the end user. What I have come up with is this:
when HTTP_REQUEST {
set loop 0
set max [llength $::shorturi]
while {$loop < $max}{
set tmpstr [lindex $::shorturi $loop]
if {[HTTP::uri] starts_with $tmpstr}{
set uri1 {findstr [http_uri] $tmpstr}
set uri2 {regexp -all -inline {$tmpstr::longuri}}
subst {[HTTP::uri] $uri2}
}
}
}
I have to Data Groups - long_uris and short_uris:
short_uris:
someuri1
someuri2
long_uris:
rewrite/someuri1
rewrite/someuri2
the end of the long uri will always match what the short uri is
I have not had a chance to test this yet - still building out the test environment, but it does not kick any errors on the LTM. I am just looking for some input - as I am not a developer (I'm a network guy).
So - to re-iterate - the long term goal is this:
user comes in to http://www.somesite.com/someuri
and the LTM rewrites the uri on the backside to:
http://www.somesite.com/rewrite/someuri (the rewrite portion is much much longer in the real data)
Thanks in advance
9 Replies
Sort By
- unRuleY_95363Historic F5 AccountI think there are a number of things that can be improved in your example. But, let's not start there yet.
class my_uri_mappings { "/index.html /some/place/for/html" "/pretty.gif /some/place/for/images" "/unknown.html /some/place/for/hackers" }
when HTTP_REQUEST { set rewrite [findclass [HTTP::uri] $::my_uri_mappings " "] if { $rewrite ne "" } { HTTP::uri [concat $rewrite [HTTP::uri]] } }
class my_uri_mappings { "/some/place/for/html/index.html" "/some/place/for/images/pretty.gif" "/some/place/for/hackers/unknown.html" }
when HTTP_REQUEST { set rewrite [matchclass $::my_uri_mappings ends_with [HTTP::uri]] if { $rewrite } { HTTP::uri [lindex $::my_uri_mappings [expr $rewrite - 1]] } }
- Spence_Faschin1
Nimbostratus
Again - this just shows why I am not a programer - I used the second example - and that works rather well in all respects except - I don't wan't the client to see the new uri - I just want it to be passed on the back end - any way we can do that? - Spence_Faschin1
Nimbostratus
I did a little more testing - works great if the uri is equal to something in the data group. In my case - if the uri starts with something in the data group, I want to rewrite that portion of the uri, example: - Spence_Faschin1
Nimbostratus
Just an update - here is the final rule that I ended up going with:when HTTP_REQUEST { if { [getfield /[findclass [findstr [http_uri] "" 1 "/"] $::uris] " " 1] ne ""}{ HTTP::uri "[findclass /[findstr [http_uri] "" 1 "/"] $::uris " " ][HTTP::uri]" } }
- JRahm
Admin
For the non-coders on this forum, (ok, idiot ME!) can you explain why that is an optimized version of the previous example? Thanks. - Spence_Faschin1
Nimbostratus
So - setting a variable before a rule matches will not cause additional overhead when processing a rule? My thought was I did not want to set a variable, unless the traffic passed my if statement - and at that point - I figured, why set a variable at all, if I can just splat it with one line. I will try the latest example once my test environment is back online and let everyone know how it goes. - unRuleY_95363Historic F5 AccountThat is a very good point. You have obviously thought about this. Of course, it will all really depend on just how often you expect to match. If it does not match often, then you are completely correct. If it matches regularly, then you would likely want to save the result in a variable. Another factor to weigh is the number of elements in the class/datagroup.
rule my_fast_rule { timing on when HTTP_REQUEST { Do some stuff } }
rule my_slow_rule { when HTTP_REQUEST timing on { Do some other stuff } }
- Spence_Faschin1
Nimbostratus
This rule will be used on a very busy e-comm type site. My understanding is they peak at 52 million unique page clicks per day, during the holiday season, with 20 million unique page clicks being the average . . . - unRuleY_95363Historic F5 AccountOk.
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