Forum Discussion

Bertolazzi_7869's avatar
Bertolazzi_7869
Icon for Nimbostratus rankNimbostratus
Apr 18, 2012

HTTP Redirect: Hint or an example?

Hi everyone !

 

Sorry for my bad English.

 

See if you can help me, I'm a newbie in F5 and iRules and I search the following:

 

Redirect requests made through the url http://www.mydomain.com:8080/ABC to url http://www.mydomain.com:8080/DEF pointing to a pool servers called SERVERS.

 

 

Does anyone have a hint or an example?

 

 

Tks.

 

 

7 Replies

  • do you want redirection or rewriting?

    Redirect vs. Rewrite

    http://cheeso.members.winisp.net/iirf20Help/html/2e820208-c2eb-4d6d-a134-c63f7d41244f.htm

    this is an example of redirection.

    [root@ve1023:Active] config  b virtual bar list
    virtual bar {
       snat automap
       pool server
       destination 172.28.19.79:8080
       ip protocol 6
       rules myrule
       profiles {
          http {}
          tcp {}
       }
    }
    [root@ve1023:Active] config  b rule myrule list
    rule myrule {
       when HTTP_REQUEST {
       if { [HTTP::uri] equals "/ABC" } {
          HTTP::redirect "http://[HTTP::host]/DEF"
       }
    }
    }
    [root@ve1023:Active] config  curl -I http://www.mydomain.com:8080/ABC
    HTTP/1.0 302 Found
    Location: http://www.mydomain.com:8080/DEF
    Server: BigIP
    Connection: Keep-Alive
    Content-Length: 0
    
    
  • Hi Bertolazzi,

    Try this:

     
    when HTTP_REQUEST {
    if { [string tolower [HTTP::uri]] starts_with "/abc" } {
    HTTP::redirect [string map {"abc" "def"} [HTTP::uri]]
    }
    }
    

    This iRule some assume that you just want to replace the sub-directory "abc" with "def" and keep the rest of the HTTP::uri.

    If you are needing something else just let us know.

    Hope this helps.
  • Nitass and Michael, tks for your reply!

     

     

    I will test now and post the results.

     

     

  • Hi,

     

     

    The redirection works fine with Nitass code!

     

     

    Please, tell me if this configuration is correct:

     

     

    http://www.mydomain.com:8080/ABC redirects to http://www.mydomain.com:8080/DEF and points to the pool pool_def

     

     

    http://www.mydomain.com:8080/HIJ redirects to http://www.mydomain.com:8080/KLM and points to the pool pool_klm

     

     

    when HTTP_REQUEST {

     

    if { [HTTP::uri] equals "/abc" } {

     

    HTTP::redirect "http://[HTTP::host]/def"{

     

    "/abc*" {pool pool_def}

     

    }

     

    }

     

    if { [HTTP::uri] equals "/hij" } {

     

    HTTP::redirect "http://[HTTP::host]/klm"{

     

    "/hij*" {pool pool_klm}

     

    }

     

    }

     

    }

     

     

     

    tks.

     

     

  • Hi,

     

     

    I searched here in the forum and found a thread on redirection to pool, so I created two iRules.

     

     

    1º - iRule - pool redirection

     

     

    when HTTP_REQUEST {

     

    switch -glob [string tolower [HTTP::uri]] {

     

    "/abc*" {pool pool_def}

     

    "/hij*" {pool pool_klm}

     

    }

     

    }

     

     

    2º iRule - http redirection

     

     

    when HTTP_REQUEST {

     

    if { [HTTP::uri] equals "/abc" } {

     

    HTTP::redirect "http://[HTTP::host]/def/"

     

    }

     

    if { [HTTP::uri] equals "/hij" } {

     

    HTTP::redirect "http://[HTTP::host]/klm/"

     

    }

     

    }

     

     

    I was successful in the tests that I performed, but i don't know if this way is the best.

     

     

    I will continue performing the tests and keep everyone updated.

     

     

    Tks.
  • i think Michael's irule is more general. by the way, you may combine two irules together.

    e.g.

    [root@ve1023:Active] config  b rule myrule list
    rule myrule {
       when HTTP_REQUEST {
       switch [HTTP::uri] {
          "/ABC" { HTTP::redirect "http://[HTTP::host]/DEF" }
          "/HIJ" { HTTP::redirect "htt://[HTTP::host]/KLM" }
          "/DEF" { pool pool_def }
          "/KLM" { pool pool_klm }
       }
    }
    }