Forum Discussion

Josh_Abaire's avatar
Josh_Abaire
Icon for Nimbostratus rankNimbostratus
Oct 23, 2012

Modify one part of URI Path

I need to change one portion of a URI path based on the value of another portion. For example:

 

 

http://www.example.com/dir1/dir2/file.ext?arg1=val1&arg2=val2&arg3

 

If dir2 then change dir1 to dir5.

 

HTTP::respond 302 Location http://www.example.com/dir5/dir2/file.ext?arg1=val1&arg2=val2&arg3

 

 

I can pull out the 2nd part:

 

 

set path2 [URI::path [HTTP::uri] 2 2]

 

I can check it against the list of values in a data-group and supply the new value but I don’t know how to modify only one portion of the URI.

 

I'm looking for a command like the set command, only rewrite URI:path portion based on an index.

 

 

Thanks!

 

 

 

4 Replies

  • is this applicable?

    [root@ve10:Active] config  b virtual bar list
    virtual bar {
       snat automap
       pool foo
       destination 172.28.19.79:80
       ip protocol 6
       rules myrule
       profiles {
          http {}
          tcp {}
       }
    }
    [root@ve10:Active] config  b class foo_class list
    class foo_class {
       "/dir2/" { "/dir1/ /dir5/" }
    }
    [root@ve10:Active] config  b rule myrule list
    rule myrule {
       when HTTP_REQUEST {
       if { [class match -- [HTTP::uri] contains foo_class] } {
          set uri [string map [class match -value [HTTP::uri] contains foo_class] [HTTP::uri]]
          HTTP::redirect "http://[HTTP::host]$uri"
       }
    }
    }
    
    [root@ve10:Active] config  curl -I "http://172.28.19.79/dir1/dir2/file.ext?arg1=val1&arg2=val2&arg3"
    HTTP/1.0 302 Found
    Location: http://172.28.19.79/dir5/dir2/file.ext?arg1=val1&arg2=val2&arg3
    Server: BigIP
    Connection: Keep-Alive
    Content-Length: 0
    
    [root@ve10:Active] config  curl -I "http://172.28.19.79/somethingelse/file.ext?arg1=val1&arg2=val2&arg3"
    HTTP/1.1 404 Not Found
    Date: Wed, 24 Oct 2012 00:18:48 GMT
    Server: Apache/2.2.3 (CentOS)
    Connection: close
    Content-Type: text/html; charset=iso-8859-1
    
    
  • Sting map! That's exactly what I needed! Thank you.

     

     

    Now I'm having problem with adding records to the data-group (tmsh v10.2). Any format or syntax I seem to use tells me there is a problem. Even ? marking my way through, it seems I am doing it right, but it is still rejected. I was able to add records through the GUI but regular use of the GUI is not practical. Even showing the data-group after adding a record from the GUI and using that format yields syntax errors. Can someone provide the correct tmsh command syntax to create the above class or add records to it?

     

     

    Thanks again!
  • It's a known tmsh bug in v10. I'm not as dumb as I look!

     

     

    http://support.f5.com/kb/en-us/solutions/public/12000/900/sol12999.html
  • Also, FYI nitass. Your solution got me where I needed, but would result in an endless 302 redirect. Since it never changes /dir2/ it will always match and attempt to redirect. You have to match both:

     

     

    "dir1/dir2/" { "/dir1/ /dir5/" }