Search
Joe Pruitt - A Software Architect's take on Network Security
You are here: DevCentral > Weblogs

posted on Wednesday, July 27, 2005 10:20 AM

In this case the user wants to change the uri of a HTTP request without forcing a redirect from the client.

We have a unique situation where a client-based DLL is uploading a file to our website. For reasons beyound my control I need to change the destination url without a redirect. When I use a redirect the payload (file to upload) is lost because the DLL is not smart enough to resubmit file for upload.

This can be done with the HTTP::uri command fairly easily. The fun starts with how you go about doing it.

Let's say you want to replace the entire URI with something new. For this example, let's say we need to replace "/foo" with "/bar". This can be done by the following:

when HTTP_REQUEST {
   if { [HTTP::uri] equals "/foo" } {
      HTTP::uri "/bar"
   }
}

But, what happens if you are working with a URI containing get parameters? This way you only know the beginning of the uri. Again, no problem. In this example the following iRule will replace any uri starting with "/foo" with "/bar"

when HTTP_REQUEST {
   if { [HTTP::uri] starts_with "/foo" } {
      HTTP::uri "/bar[string range $uri 4 end]"
   }
}

-Joe

[Listening to: Nookie - Limp Bizkit - Significant Other (04:50)]

Posted In: iRules,

Feedback

1/11/2008 12:46 PM
Gravatar Is the second example missing a variable assignment or is HTTP::uri assigned to $uri automagically?

For anyone else who didn't grok "string range $uri 4 end" here's the TCL doc explaining it.
http://www.astro.princeton.edu/~rhl/Tcl-Tk_docs/tcl8.0a1/string.n.html
Dave Noonan
1/11/2008 1:51 PM
Gravatar It should work as is. The HTTP::uri command takes two forms, if enclosed in braces with no arguments, it returns the URI. If it is passed a parameter then it uses the passed in variable as the new URI.

In the second example, the line

HTTP::uri "/bar[string range $uri 4 end]"

is a URI assignment with the new value being "/bar" concatenated with the 5th character on in the URI.

-Joe
Joe Pruitt
7/13/2008 8:08 AM
Gravatar I want to replace the URL with the new URL

So when server respond to the user query LTM should rewrite the entire URL with new one and send across to use as HTTP response
Dinesh Mohite
7/17/2008 6:57 PM
Gravatar How about if the original request did not have a uri but you want to insert one to the loadbalanced servers?

Original request:
server.mydomain.com

redirect to:
x.x.x.x/dir/link
Steve Russo
7/21/2008 11:29 AM
Gravatar "I want to replace the URL with the new URL

So when server respond to the user query LTM should rewrite the entire URL with new one and send across to use as HTTP response"

Bump.

I can find no mechanism to do this, at least in the v9.3.1 docs. Rewriting the URI is fine, but how 'bout for an app that can't handle 302 redirects, for which I want the F5 to take all connections to a virtual and send them off to "bar.foo.com"?
Joe P.
7/21/2008 11:36 AM
Gravatar There are two ways to modify the URI.

1) HTTP redirects. This will send a redirect HTTP response to the client with the Location response header containing the new location. The browser will update the address bar with the new location and issue a new request.

2) Transparent header modifications. You can modify the HTTP::uri and HTTP::host values in an iRule without a redirect to the browser. This will make it appear to the server that a different request was made and the browser will not update it's address bar (since a redirect command wasn't made. So, if you want http://www.foo.com/file1.txt to "look like" http://www.bar.com/file2.txt, you can do so with the following iRules code

when HTTP_REQUEST {
if { ([HTTP::host] eq "www.foo.com") && ([HTTP::uri] eq "/file1.txt" } {
HTTP::host "www.bar.com"
HTTP::uri "/file2.txt"
}
}

Hope this helps...

Feel free to post more questions over on the iRules forum if they come up.

-Joe
Joe Pruitt
7/21/2008 3:24 PM
Gravatar ORLY?

BIGpipe: rule modification error:
01070151:3: Rule [my_rewrite] error:
line 2: [wrong # args] [HTTP::host "foo.bar.com"]

Moved to here:
http://devcentral.f5.com/Default.aspx?tabid=53&forumid=5&postid=26209&view=topic
Joe P.
7/21/2008 4:00 PM
Gravatar Doh, I always forget that HTTP::host doesn't have a write method. This should do it for you.

when HTTP_REQUEST {
if { ([HTTP::host] eq "www.foo.com") && ([HTTP::uri] eq "/file1.txt" } {
HTTP::header replace "Host" "www.bar.com"
HTTP::uri "/file2.txt"
}
}

-Joe
Joe Pruitt
7/23/2008 4:17 AM
Gravatar Hi,
i need irule to redirect new URL , when URI start with /support.
Anbarasan
7/23/2008 9:18 AM
Gravatar Here you go:

when HTTP_REQUEST {
if { [HTTP::uri] starts_with "/support" } {
HTTP::redirect "http://newhost/newuri.ext"
}
}

-Joe
Joe Pruitt
7/29/2008 6:57 AM
Gravatar Hi,
my english is pretty bad, and i'm not sure my question is placed right here.
i need to redirect a form with post parameters. this irule works but doesn't send the post params.

when HTTP_REQUEST {
if {[HTTP::uri] contains {abc-anfrage}}
{HTTP::uri {www.example.com/abc-Anfrage}
}
}

Any help appreciated.
Marco
Marco
9/10/2008 6:08 AM
Gravatar Hi

I have a url say for example www.abcxyz.com. If the url is accessed by external user i want it redirected to www.abc123.com. But if the url is accessed from internal users i DO NOT want a redirect. Are you able to help as to how can i can create a irule to be used based on source ip.

Thanks
Pag
9/26/2008 6:45 AM
Gravatar Hmm. I've done what I think is the right thing from looking through the docs and from these forums.

when HTTP_REQUEST {
if { [HTTP::uri] equals "/" } {
set newuri "/APP/signon.html"
log "Incoming uri - [HTTP::uri] being translated to $newuri"
HTTP::uri $newuri
}
}

What I see when I go to https://host.domain/ is the following:

https://host.domain/
https://host.domain/psp/APP/signon.html
https://host.domain/psp/psp/APP/signon.html
https://host.domain/psp/psp/psp/APP/signon.html

and so on until I stop the browser. This is from the log:

Fri Sep 26 09:23:15 EDT 2008 tmm tmm[1124] 01220002 Rule rewrite_login_url HTTP_REQUEST: Incoming uri - /psp/APP/?cmd=login being translated to /APP/signon.html
Fri Sep 26 09:23:14 EDT 2008 tmm tmm[1124] 01220002 Rule rewrite_login_url HTTP_REQUEST: Incoming uri - / being translated to /APP/signon.html

The thing is I forgot the "if URI equals "/". Then I added it in to the iRule, but it still seems to be using the old one. Is there some magic (as a newbie) I'm missing to making the new iRule active?

Don
Don Badrak
9/26/2008 7:00 AM
Gravatar Hmm. I've done what I think is the right thing from looking through the docs and from these forums.

when HTTP_REQUEST {
if { [HTTP::uri] equals "/" } {
set newuri "/APP/signon.html"
log "Incoming uri - [HTTP::uri] being translated to $newuri"
HTTP::uri $newuri
}
}

What I see when I go to https://host.domain/ is the following:

https://host.domain/
https://host.domain/psp/APP/signon.html
https://host.domain/psp/psp/APP/signon.html
https://host.domain/psp/psp/psp/APP/signon.html

and so on until I stop the browser. This is from the log:

Fri Sep 26 09:23:15 EDT 2008 tmm tmm[1124] 01220002 Rule rewrite_login_url HTTP_REQUEST: Incoming uri - /psp/APP/?cmd=login being translated to /APP/signon.html
Fri Sep 26 09:23:14 EDT 2008 tmm tmm[1124] 01220002 Rule rewrite_login_url HTTP_REQUEST: Incoming uri - / being translated to /APP/signon.html

The thing is I forgot the "if URI equals "/". Then I added it in to the iRule, but it still seems to be using the old one. Is there some magic (as a newbie) I'm missing to making the new iRule active?

Don
Don Badrak
9/26/2008 9:06 AM
Gravatar Hey Dan, that seems odd. The "equals" comparison should not succeed for the uri of "/psp/App/?cmd=login" as shown in your first log statement. Move this over to the iRules forum and we can diagnose it further.
http://devcentral.f5.com/Default.aspx?tabid=53&view=topics&forumid=5


Joe Pruitt
5/28/2010 10:44 PM
Gravatar
Hello all, i am trying to a get a hang of this i am very new to the whole i rule thing. at any rate we have some url i am interested in first of all doing a redirect for any thing that comes in http to be https , then i need to have say https://abc.com/123 be redirected to https://abc.com/123/login.do .. will what i have get me closer to making that work.. thanks in advance..

when {HTTP::request {
if { ([HTTP::host] eq "abc.com") && (HTTP::uri] eq"/123/" } {
HTTP::redirect "https://abc.com/123/login.do
}
}
Al
9/29/2010 2:43 PM
Gravatar 5-Minute iRules: What
Jason Rahm
12/7/2010 4:23 AM
Gravatar casino online gambling
I was very pleased to find such wonderful source of info as your site is! I wanted to thank you for this great read!! I definitely enjoyed every post I managed to go through.
..
shimli
12/22/2010 3:02 PM
Gravatar I need to add a context to the URL if none specifed:

change: http://xyz.com
to: http://xyz.com/support (added /support)

How/where to make this change in the F5 configuraton?

Please advise.
Thanks,
Paul
Paul Thakur
5/2/2011 8:15 AM
Gravatar Hi please help me, my iRule is this

when {HTTP::request {
if { ([HTTP::host] eq "mail.alumno.bbc.com") } {
HTTP::redirect "http://www.outlook.com/alumno.bbc.com"
}
}

when I start it say me

01070088:3: The requested object name (Redirect alumno) is invalid.

what is it wrong?
Jessy
5/2/2011 1:41 PM
Gravatar Jessy, when you said you started it, what did that mean. did you get the error when saving the iRule or was that error put in the log when a request was made through the Virtual Server using that iRule? I don't see anything wrong with your iRule as it's written to cause that error. Especially since I don't see the token "alumno" anywhere outside of the middle of the URL in the redirect statement.

-Joe
Joe
2/9/2012 4:37 AM
Gravatar Hey I am looking for I rule where request

http://abc.com should redirect to www.abc.com
Also when there is request like
http://abc.com/home/xyz or www.abc.com/home/xyz it should redirect to

www.abc.com/xyz
Pankaj

Let Me Know What You Think


Please use the form below if you have any comments, questions, or suggestions.

Title:
 
Name:
 
Email: (so we can show your gravatar)
Website:
Comment: Allowed tags: blockquote, a, strong, em, p, u, strike, super, sub, code
 
Please add 8 and 1 and type the answer here:

Blog Stats

Posts:379
Comments:1067
Stories:1
Trackbacks:301
  

Article Categories

  iRules
  

Image Galleries

  

Joe's bookshelf: read

The Lost Gate
4 of 5 stars
This one started slow but I got really got into it about 1/3 of the way through. If you are an Ender's Game fan, you'll probably like this one as well.

goodreads.com


82,243 Members in 102 Countries and Growing!

Join DevCentral Today!

About DevCentral

DevCentral has been a successful, thriving community for many years. We have always strived to bring you the best technical documentation, discussion forums, blogs, media and much more that we can.

So dive in, get familiar with DevCentral. We hope you like it, we hope it makes your job easier, and lets you get that much more power out of the community. To learn more, make sure to check out the Getting Started section. And if you have any problems, or think something could be easier to use, drop us a line to let us know.

Got It !

We've received your comment and transmitted it directly to DevCentral HQ.

Thanks for taking time to let us know what's on your mind. At DevCentral | Community Matters!

Get In Touch With Us

Have questions, suggestions or just want to get something off your chest?

Use our handy form below to Direct Connect with DevCentral Mission Control.

Send Us Feedback       or