Forum Discussion

Mike_129800's avatar
Mike_129800
Icon for Nimbostratus rankNimbostratus
Jul 07, 2013

Redirect to different host and port based on URL

Hi

 

We just installed two F5 BigIP (LTM) devices so I am new to iRules, how would I setup the following redirect based on the incoming URL?

 

https://host.com/product1 redirect to https://host1.com:6500/product

 

https://host.com/product2 redirect to https://host2.com:6500/product

 

https://host.com/product3 redirect to https://host3.com:6500/product

 

https://host.com/product4 redirect to https://host4.com:6500/product

 

I was looking at doing the following for the URL rewrite but I am not sure how to get the hostname and port change part of the rewrite:

 

-----

 

when HTTP_REQUEST {

 

if { [string tolower [HTTP::uri]] starts with "/product1"}{

 

HTTP::uri [string map -nocase {product1 product} [HTTP::uri]]

 

}

 

if { [string tolower [HTTP::uri]] starts with "/product2"}{

 

HTTP::uri [string map -nocase {product2 product} [HTTP::uri]]

 

}

 

if { [string tolower [HTTP::uri]] starts with "/product3"}{

 

HTTP::uri [string map -nocase {product3 product} [HTTP::uri]]

 

}

 

if { [string tolower [HTTP::uri]] starts with "/product4"}{

 

HTTP::uri [string map -nocase {product4 product} [HTTP::uri]]

 

}

 

}

 

------

 

Thank you!

 

Mike

 

3 Replies

  • i think you mean host and uri rewriting rather than redirection.

    Redirects, Rewrites and App Transfers via iRules by Colin Walker

    https://devcentral.f5.com/tech-tips/articles/redirects-rewrites-and-app-transfers-via-irules

    e.g.

    [root@ve10:Active] config  b virtual bar list
    virtual bar {
       snat automap
       destination 172.28.19.252:80
       ip protocol 6
       rules myrule
       profiles {
          http {}
          tcp {}
       }
    }
    [root@ve10:Active] config  b rule myrule list
    rule myrule {
       when HTTP_REQUEST {
      switch [string tolower [HTTP::uri]] {
        /product1 { set newhost "host1.com";set newnode "200.200.200.101" }
        /product2 { set newhost "host2.com";set newnode "200.200.200.102" }
        /product3 { set newhost "host3.com";set newnode "200.200.200.103" }
        /product4 { set newhost "host4.com";set newnode "200.200.200.104" }
        default {
           do something
        }
      }
      HTTP::header replace Host $newhost
      HTTP::uri "/product"
      node $newnode 6500
    }
    }
    
    [root@ve10:Active] config  ssldump -Aed -nni 0.0 port 80 or port 6500
    New TCP connection 1: 172.28.20.17(38342) <-> 172.28.19.252(80)
    1373241702.9550 (0.0010)  C>S
    ---------------------------------------------------------------
    GET /product1 HTTP/1.1
    User-Agent: curl/7.15.5 (i686-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
    Accept: */*
    Host:host.com
    
    ---------------------------------------------------------------
    
    New TCP connection 2: 200.200.200.10(38342) <-> 200.200.200.101(6500)
    1373241702.9562 (0.0011)  C>S
    ---------------------------------------------------------------
    GET /product HTTP/1.1
    User-Agent: curl/7.15.5 (i686-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
    Accept: */*
    Host:host1.com
    
    ---------------------------------------------------------------
    
  • Thank you, so something like this for the iRule:

    
     when HTTP_REQUEST {
      switch [string tolower [HTTP::uri]] {
        /sto { set newhost "sto.domain";set newnode "200.200.200.101" }
        /cph { set newhost "cph.domain";set newnode "200.200.200.102" }
        /bru { set newhost "bru.domain";set newnode "200.200.200.103" }
        /jnb { set newhost "jnb.domain";set newnode "200.200.200.104" }
        default {
           do something
        }
      }
      HTTP::header replace Host $newhost
      HTTP::uri [string map -nocase {sto WOS} [HTTP::uri]]
      HTTP::uri [string map -nocase {cph WOS} [HTTP::uri]]
      HTTP::uri [string map -nocase {bru WOS} [HTTP::uri]]
      HTTP::uri [string map -nocase {jnb WOS} [HTTP::uri]]
      node $newnode 6500
    }
    

    I want to only replace part of the URL i.e. sto, cph, bru, jnb with WOS will the above work for that?

    The set newnode "200.200.200.101" should that be the IP of new host or is it something else?

    What is the virtual bar config for in the above example?

    I am setting this up via the GUI.

    Thanks,

    Mike
  • I want to only replace part of the URL i.e. sto, cph, bru, jnb with WOS will the above work for that?e.g.

    [root@ve10:Active] config  b rule myrule list
    rule myrule {
       when HTTP_REQUEST {
      switch [string tolower [HTTP::uri]] {
        /sto { set newhost "sto.domain";set newnode "200.200.200.101" }
        /cph { set newhost "cph.domain";set newnode "200.200.200.102" }
        /bru { set newhost "bru.domain";set newnode "200.200.200.103" }
        /jnb { set newhost "jnb.domain";set newnode "200.200.200.104" }
        default {
           do something
        }
      }
      HTTP::header replace Host $newhost
      HTTP::uri [string map {sto WOS cph WOS bru WOS jnb WOS} [HTTP::uri]]
      node $newnode 6500
    }
    }
    
    [root@ve10:Active] config  ssldump -Aed -nni 0.0 port 80 or port 6500
    New TCP connection 1: 172.28.20.17(38378) <-> 172.28.19.252(80)
    1373275574.5080 (0.0007)  C>S
    ---------------------------------------------------------------
    GET /sto HTTP/1.1
    User-Agent: curl/7.15.5 (i686-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
    Accept: */*
    Host: host.com
    
    ---------------------------------------------------------------
    
    New TCP connection 2: 200.200.200.10(38378) <-> 200.200.200.101(6500)
    1373275574.5103 (0.0010)  C>S
    ---------------------------------------------------------------
    GET /WOS HTTP/1.1
    User-Agent: curl/7.15.5 (i686-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
    Accept: */*
    Host: sto.domain
    
    ---------------------------------------------------------------
    

    you may use -glob switch option in case uri is not exact uri.

    iRules 101 - 04 - Switch by Joe Pruitt

    https://devcentral.f5.com/tech-tips/articles/irules-101-04-switch

    The set newnode "200.200.200.101" should that be the IP of new hostyes

    What is the virtual bar config for in the above example?it is standard virtual server with http profile. there is no pool because node command is used to send traffic to pool member.