Forum Discussion
DaveC_21078
Altostratus
Aug 27, 2009How can I chab=nge the format of a URL?
I need to be able to search for a URL similar to
http://example.com/adserver/impression/pid=123/oid=860/rand=12345/?click=http://www.publisher.com/track/ad.aspx?click=1&test=2&imp=1
...
Aug 28, 2009
Yeah, that one burned me a long time ago and forgot about it. the "string map" command performs a literal replacement (ie. it doesn't do variable substitution). So in your example it was looking for the literal string "adserver/impression/pid=$pidnum/oid=$oidnum/rand=$randnum/?click" instead of the correct "adserver/impression/pid=123/oid=860/rand=12345/?click" Same goes for the replacement string.
Also, your second log statement as a typo with the $oidnumm variable which may throw an error.
The good news about the variable substitution is that you can fix that by wrapping the map strings with a "subst" command. This should work for you:
when HTTP_REQUEST {
if { [HTTP::uri] contains "adserver/impression" }{
set found [scan [HTTP::uri] "/adserver/impression/pid=%d/oid=%d/rand=%d" pidnum oidnum randnum]
if { $found == 3 } {
log local0. "This is the URI = [HTTP::uri]"
log local0. "Pid = $pidnum, Oid = $oidnum, rand = $randnum"
set newuri [string map [subst {"adserver/impression/pid=$pidnum/oid=$oidnum/rand=$randnum/?click" "ad.imp?pid=$pidnum&oid=$oidnum&rand=$randnum/?pclk"}] [HTTP::uri]]
log local0. "New URI: $newuri";
HTTP::uri $newuri;
}
}
}
I threw in a sanity check for the scan to verify that it did indeed find the three values and I also put in the newly mapped URI into a variable and logged it so you can verify in the BIG-IP logs whether the "string map" worked. Oh, and I removed the $uri variable for optimization. You can remove the $newuri variable after you've tested and it works.
A fully optimized iRule would be this:
when HTTP_REQUEST {
if { [HTTP::uri] contains "adserver/impression" }{
set found [scan [HTTP::uri] "/adserver/impression/pid=%d/oid=%d/rand=%d" pidnum oidnum randnum]
if { $found == 3 } {
log local0. "This is the URI = [HTTP::uri]"
log local0. "Pid = $pidnum, Oid = $oidnum, rand = $randnum"
HTTP::uri [string map [subst {"adserver/impression/pid=$pidnum/oid=$oidnum/rand=$randnum/?click" "ad.imp?pid=$pidnum&oid=$oidnum&rand=$randnum/?pclk"}] [HTTP::uri]]
}
}
}
Hope this helps!
-Joe
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