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
and manipulte it to look like
http://example.com/ad.imp?pid=123&oid=860&rand=12345/?pclk=http://www.publisher.com/track/ad.aspx?click=1&test=2&imp=1
before passing it on. Can I do this with an iRule?
Thanks for looking.
19 Replies
- DaveC_21078
Altostratus
I appreciate the input, but no change. I've simplified this thing as much as I can, but no joy. It is still passing the original URL. I tired
priority 501
when HTTP_REQUEST {
if { [HTTP::uri] contains "adserver/impression" }{
set uri [HTTP::uri]
scan $uri "adserver/impression/pid=%d/oid=%d/rand=%d" pidnum oidnum randnum
HTTP::uri [string map {"adserver/impression/pid=$pidnum/oid=$oidnum/rand=$randnum/?click" "ad.imp?pid=$pidnum&oid=$oidnum&rand=$randnum/?pclk"} [HTTP::uri]]
}}
and
when HTTP_REQUEST {
if { [HTTP::uri] contains "adserver/impression" }{
set uri [HTTP::uri]
scan $uri "adserver/impression/pid=%d/oid=%d/rand=%d" pidnum oidnum randnum
HTTP::uri [string map {"adserver/impression/pid=$pidnum/oid=$oidnum/rand=$randnum/?click" "ad.imp?pid=$pidnum&oid=$oidnum&rand=$randnum/?pclk"} [HTTP::uri]]
}} - hoolio
Cirrostratus
String map won't work with variables wrapped in curly braces. So you can either use subst like Joe listed, or remove the curly braces. Also, Citizen's suggestion was to add a second HTTP request event with a priority of 501 so you could see the effect of the change once it is made. And you'll need to include a leading forward slash in the scan pattern or it won't match any URI.
Can you try this and reply with the log output from /var/log/ltm if it doesn't work expected?when HTTP_REQUEST { log local0. "[IP::client_addr]:[TCP::client_port]: New HTTP request to [HTTP::uri]" if { [HTTP::uri] contains "adserver/impression" }{ log local0. "[IP::client_addr]:[TCP::client_port]: Matched URI check" Scan the URI looking for the pid, oid and rand values if { [scan [HTTP::uri] "/adserver/impression/pid=%d/oid=%d/rand=%d" pid oid rand] == 3 } { log local0. "[IP::client_addr]:[TCP::client_port]: Scanned three values: pid = $pid, oid = $oid, rand = $rand" HTTP::uri [string map "adserver/impression/pid=$pid/oid=$oid/rand=$rand/?click ad.imp?pid=$pid&oid=$oid&rand=$rand/?pclk" [HTTP::uri]] } } } when HTTP_REQUEST priority 501 { log local0. "[IP::client_addr]:[TCP::client_port]: 501: Updated URI: [HTTP::uri]" }
Aaron - DaveC_21078
Altostratus
Aaron,
Thanks for the reply. Output of /var/log/ltm below after copying your text to the irule, ads.imp.
Sep 1 07:37:11 tmm tmm[1128]: Rule ads.imp : 172.16.102.231:1158: 501: Updated URI: /adserver/impression/pid=4/oid=3/rand=%5Btimestamp%5D/?click=
Sep 1 07:37:12 tmm tmm[1128]: Rule ads.imp : 172.16.102.231:1158: New HTTP request to /adserver/impression/pid=4/oid=3/rand=%5Btimestamp%5D/?click=
Sep 1 07:37:12 tmm tmm[1128]: Rule ads.imp : 172.16.102.231:1158: Matched URI check
Sep 1 07:37:12 tmm tmm[1128]: Rule ads.imp : 172.16.102.231:1158: 501: Updated URI: /adserver/impression/pid=4/oid=3/rand=%5Btimestamp%5D/?click=
Sep 1 07:37:13 tmm tmm[1128]: Rule ads.imp : 172.16.102.231:1158: New HTTP request to /adserver/impression/pid=4/oid=3/rand=%5Btimestamp%5D/?click=
Sep 1 07:37:13 tmm tmm[1128]: Rule ads.imp : 172.16.102.231:1158: Matched URI check
Sep 1 07:37:13 tmm tmm[1128]: Rule ads.imp : 172.16.102.231:1158: 501: Updated URI: /adserver/impression/pid=4/oid=3/rand=%5Btimestamp%5D/?click=
Sep 1 07:37:14 tmm tmm[1128]: Rule ads.imp : 172.16.102.231:1158: New HTTP request to /adserver/impression/pid=4/oid=3/rand=%5Btimestamp%5D/?click=
Sep 1 07:37:14 tmm tmm[1128]: Rule ads.imp : 172.16.102.231:1158: Matched URI check
Sep 1 07:37:14 tmm tmm[1128]: Rule ads.imp : 172.16.102.231:1158: 501: Updated URI: /adserver/impression/pid=4/oid=3/rand=%5Btimestamp%5D/?click= - DaveC_21078
Altostratus
Aaron,
It's working. My url was the problem, it had "rand=%5Btimestamp%5D", which the IF statement apparently didn't like. I changed it to a and then the IF starement worked. The only problem I have now is that the iRule replaces the "?" after ad.imp with a space. Otherwise, the new URI looks perfect.
Thanks for all your help.
Dave - hoolio
Cirrostratus
Hi Dave,
Glad to see you're making progress. Can you post the updated iRule and logs which show the new issue?
Thanks,
Aaron - DaveC_21078
Altostratus
Aaron,
Iit's the same iRule with the same results that I sent you earlier. The URL I was using to test was the problem. The If staement was failing because it didn't like "rand=%5Btimestamp%5D". The URL is below. I changed the URL to rand=5 and it works fine. The space I saw ad.imp was from IIS, not the BigIP. Is there a way to alter the rule to get it to accept and rewrite the URI with that string, which we are going to see occasionally?
adserver/impression/pid=4/oid=3/rand=%5Btimestamp%5D/?click=
Dave - hoolio
Cirrostratus
The scan command is expecting a (set of) digit characters. So you could change the scan to match any character which is not a / instead:if { [scan [HTTP::uri] "/adserver/impression/pid=%s/oid=%s/rand=%s" pid oid rand] == 3 } {
to:if { [scan [HTTP::uri] {/adserver/impression/pid=%[^/]/oid=%[^/]/rand=%[^/]} pid oid rand] == 3 } {
Note the use of curly braces to prevent the [ ]'s from being interpreted as TCL commands.
Aaron - DaveC_21078
Altostratus
Aaron,
You are the Man! This works perfectly. I tried replacing the %d with %s, but that failed completely, I assume because of lack of white space.
I really appreciate all your work and instruction.
Dave - hoolio
Cirrostratus
Glad to see you got it working. Thanks to cmbhatt, Joe and Citizen as well for their suggestions.
Aaron
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
