Simple iRule for URL appending?

Hey - was wondering if someone could give me a hand with a pretty basic task… how to append(or redirect) and URL. I have some people comming into a VS for “host.unit.com”, but I have to send them to “host.unit.com/somewhere/else”. The VS is taking in HTTP port 80, but is doing PAT to different nodes on different ports. I tried a couple of iRule examples, but failed. Anything would be appreciated… thanks.

This is a simple version that will do the initial redirect.

when HTTP_REQUEST {
if { [HTTP::path] equals "/" } {
HTTP::redirect "/somewhere/else.html"
}
}

Let us know if this takes care of what you need or if you need anything else.

Most Excellent!!! Worked like a charm. Thanks.

This is a simple version that does what you are describing that you need.

when HTTP_REQUEST {
if { [HTTP::path] equals "/" } {
HTTP::redirect "/somewhere/else.html"
}
}

If you are needing something in addition to this or different functionality let us know.

Keep in mind that HTTP::redirect issues a 302 - temporary redirect. It’s more efficient to use 301s (permanent redirect) since it’s cached by the client.

If you’re pretty certain that the redirect target is not likely to change I’d use a 301. Otherwise you’ll want to stick with a 302.

I have an very similar request which is that instead of append uri after url, I need insert “/aaa/bbb” in between. e.g. when user type in “http://www.testdomain.com/\", my F5 iRule need insert /aaa/bbb/ and make it "http://www.testdomain.com/aaa/bbb/\”. Looking forward your kindly reply.

Not sure if “*” is the correct wildcard in TCL scripting, but when I put in “*”, I actually mean that user could be puting different value.

Thanks

Roger

Then you’d want something like:

when HTTP_REQUEST {
  if {!([HTTP::uri] starts_with "/aaa/bbb")} {
    HTTP::redirect "http://www.testdomain.com/aaa/bbb[HTTP::uri]"
  }
}

Colin

yes, this is what I put in right now:

when HTTP_REQUEST {

if {[HTTP::path] matches_regex “/*”} {

HTTP::redirect “/aaa/bbb[HTTP::uri]”

}

}

I did a packet capture on the VIP and by follow TCP stream, I find this iRule will insert /aaa/bbb with every HTTP request dose not matter it have /aaa/bbb or not. It will just insert /aaa/bbb over and over without stop, so the HTTP GET becomes…

1. GET /aaa/bbb/test.html

2. GET /aaa/bbb/aaa/bbb/test.html

3. GET /aaa/bbb/aaa/bbb/aaa/bbb/test.html

…

…

n. GET /aaa/bbb/aaa/bbb/…/aaa/bbb/aaa/bbb/test.html

Is there anything I can do in this iRule to make it insert only once?

Thanks

Roger

The rule I wrote earlier does that check. The line that’s important is:

if {!([HTTP::uri] starts_with "/aaa/bbb")} {

This means that the redirect only occurs if the URI doesn’t already start with “/aaa/bbb”

Colin

I’m glad you got it working. :slight_smile:

Colin

Hey guys,

This iRule works perfectly for me:

when HTTP_REQUEST {
if { [HTTP::path] equals "/" } {
HTTP::redirect "/apath"
}
}

But before using it I tried the following and it didn’t work but I still have the curiosity to know why if anyone has any idea why please let me know. Here’s my iRule:

when HTTP_REQUEST {
set val [HTTP::uri]
if { {[string length $val] == 0} or {[string length $val] == 1} } {
HTTP::redirect https://[HTTP::host]/apath
}
}

Did the request contain a query string?

Banyan,

[root@iris:Active] css  b virtual bar list
bvirtual bar {
   snat automap
   pool foo
   destination 172.28.17.33:http
   ip protocol tcp
   rules myrule
   profiles {
      http {}
      tcp {}
   }
}
[root@iris:Active] css  b rule myrule list
rule myrule {
   when HTTP_REQUEST {
        set val [HTTP::uri]
        log local0. "\[HTTP::uri\] is $val"
        log local0. "\[string length $val\] is [string length $val]"
        if { ([string length $val] == 0) or ([string length $val] == 1)} {
                HTTP::redirect https://[HTTP::host]/apath
        }
}
}

[root@iris:Active] css  curl -I http://172.28.17.33/
HTTP/1.0 302 Found
Location: https://172.28.17.33/apath
Server: BigIP
Connection: Keep-Alive
Content-Length: 0

[root@iris:Active] css  cat /var/log/ltm
Nov  2 20:38:32 local/tmm info tmm[1609]: Rule myrule : [HTTP::uri] is /
Nov  2 20:38:32 local/tmm info tmm[1609]: Rule myrule : [string length /] is 1

I have box in pool member node on http://xyz.com/test. I want to irule hide internal /test and redirect to only http://xyz.com. Please help me

I have box in pool member node on .xyz Domain Names | Join Generation XYZ. I want to irule hide internal /test and redirect to only http://xyz.com. Please help me

There are a few considerations to your request:

1. If you JUST want to hide the /test URI from the client, you can use the HTTP::uri command to transparently change the URI in the HTTP request.

when HTTP_REQUEST {
     HTTP::uri "/test[HTTP::uri]"
}

This is a rather brute force way of changing the incoming URI to “/test” plus whatever is already in the client’s request.

Ex. client requests “/foo” and the server sees “/test/foo”

2. The problem with the above approach is that it doesn’t account for what information the server may send back to the client in the form of object references. If that is a concern, then there are a few options, but I’d recommend an iRule-based stream expression to replace “/test” references in the response payload. The wiki page for streams has some pretty good examples that modify content in the HTTP_RESPONSE event.

3. The problem with the above is that it doesn’t account for complex applications. Your example only shows one URI, but if that’s just for illustration purposes and your environment is much more complicated than that, then I’d recommend a ProxyPass solution. The ProxyPass iRule will do exactly what you require but can handle much more complex requirements. If that’s an option, do a quick search on this site for “ProxyPass”.

Hi please help for Https request, what changes be there in this irule--

when HTTP_REQUEST { if { [HTTP::path] equals “/” } { HTTP::redirect “/somewhere/else.html” } }

for http request its working fine, but for https its not aking this irule…i changes http to https everywhere in irule…

Hi,

With https you don’t need to change anything on this iRule.

You have to apply your server-ssl profile and that’s all (you may need a client-ssl profile too).

Hi,

With https you don’t need to change anything on this iRule.

You have to apply your server-ssl profile and that’s all (you may need a client-ssl profile too).

Thanks very much Thomas Gobet…:slight_smile: