Forum Discussion

Snowman_108161's avatar
Snowman_108161
Icon for Nimbostratus rankNimbostratus
Aug 03, 2012

Looking to separate out URL from URI

Does anyone have some advice on how to pull out just the URL from a full URI? I've attempted to use HTTP::path but that pulls out both the URL and URN. An example may help to illustrate this.

 

 

I have several directories on a webserver

 

 

 

server1.corp.com/abc/admin

 

server1.corp.com/abc/user

 

 

 

server1 is accessible from the internet, I have an external NAT setup to point to the F5 which we're using as a reverse proxy and I want to limit web requests to only go to "server1.corp.com/abc/user" and not /abc/admin. That should be easy enough right? ... Well they share common .css and .js files in /abc

 

 

 

So what I'm looking to accomplish is allow requests with "/abc/user", "/abc/." but not "/abc/admin" or "/abc/*/."

 

 

Hopefully that notation makes sense... basically the files in /abc are fair game but additional directories aren't.

 

 

 

This is the basic start i have already, obviously it doesn't quite accomplish what i'm looking to do yet. If I checked again /abc/user as the path in the if statement I'd lose all formatting on the page.

 

 

 

"server1.corp.com"

 

{

 

if { not ([string tolower [HTTP::path]] starts_with "/abc") } {

 

HTTP::path "/abc/user"

 

}

 

}

 

 

2 Replies

  • Richard__Harlan's avatar
    Richard__Harlan
    Historic F5 Account
    Use HTTP::uri it does what you are looking for

     

     

    https://devcentral.f5.com/wiki/iRules.HTTP__uri.ashx

     

     

    then setup a switch statement

     

     

    switch [HTTP::uri] {

     

    "/abc/user" {

     

    use pool * }

     

    "/abc/" {

     

    use pool * }

     

    default {

     

    HTTP::redirect "http://server1.corp.com/abc/user"

     

    }

     

    }

     

     

    That should work for you

     

     

  • Nailed it finally....

    HTTP::path -- that just returns the path without the query string

    HTTP::uri -- returns the path + the query string

    I actually wanted to just use HTTP::path afterall. here's how I did it...

    when HTTP_REQUEST {

    set host [HTTP::host]

    set uri_list [split [string tolower [HTTP::path]] /]

    if { [lindex $uri_list 2] equals "admin"} {

    HTTP::respond 302 Location "https://$host/user"

    }

    }

    Basically it just redirect the client to the "/user" directory if they try and access the "/admin" directory. The real key item here is the use of TCL lists and "split". I got in this mentality while working with python and using their lists frequently to solve problems like this.

    "lindex" just returns the item at that index location and acts the exact same way as you'd expect "list[2]" to work in C or C++

    This is also another valuable little snippet so that you can get the size of the list (all the different locations in the path + the filename) and then dump the actual filename in the path.

    set list_size [llength [split [HTTP::uri] /]]

    if{ $list_size > 1 } {

    log local0. "Last item in the list: [lindex $uri_list [expr $list_size -1]]"

    }