Forum Discussion

CraigyB_172365's avatar
CraigyB_172365
Icon for Nimbostratus rankNimbostratus
Jan 12, 2018

HOST and partial path rewrite

Hi I am new to F5 and am trying to do a URL rewrite a chunk of the URL. I think I am on the right track but I just cannot make it work. Any help is really appreciated.

 

I just want to replace any request that contains the below retaining the remainder of the URL represented by *

 

https://my.server.net/info/*

 

with

 

http://10.10.10.10:9000/files/*

 

He is my effort at achieving this.

 

when HTTP_REQUEST { if { [HTTP::uri] starts_with "/info"}{ HTTP::redirect "http://[string map {https://my.server.net/info http://10.10.10.10:9000/jenkins_files} [string tolower[HTTP::host]][HTTP::uri]]" } }

 

10 Replies

  • Hello CraigyB, Try using the following iRule.

    when HTTP_REQUEST {
    if { ( [string tolower [HTTP::uri]] starts_with "/info" ) } {
        set uri [HTTP::uri [string map {"/info" "/files"} [HTTP::uri]]]
        HTTP::redirect "https://[HTTP::host]$uri"
    }
    }
    
    • CraigyB_172365's avatar
      CraigyB_172365
      Icon for Nimbostratus rankNimbostratus

      Hi Kolom, appreciate your input, which parts do i replace with destination host name and port number?

       

    • kolom's avatar
      kolom
      Icon for Altostratus rankAltostratus

      i've tested the following and it's working fine.

      when HTTP_REQUEST 
      if { ( [string tolower [HTTP::uri]] starts_with "/info" ) } {
      set newUri [string map [list "info" "files"] [HTTP::uri]]
      HTTP::redirect "http://[HTTP::host]$newUri"
      }
      

      if you want to replace the host/port , change the redirection part from HTTP::redirect "http://[HTTP::host]$newUri" to HTTP::redirect "http://10.10.10.10:9000$newUri"

  • Hello CraigyB, Try using the following iRule.

    when HTTP_REQUEST {
    if { ( [string tolower [HTTP::uri]] starts_with "/info" ) } {
        set uri [HTTP::uri [string map {"/info" "/files"} [HTTP::uri]]]
        HTTP::redirect "https://[HTTP::host]$uri"
    }
    }
    
    • CraigyB_172365's avatar
      CraigyB_172365
      Icon for Nimbostratus rankNimbostratus

      Hi Kolom, appreciate your input, which parts do i replace with destination host name and port number?

       

    • kolom_265617's avatar
      kolom_265617
      Icon for Cirrostratus rankCirrostratus

      i've tested the following and it's working fine.

      when HTTP_REQUEST 
      if { ( [string tolower [HTTP::uri]] starts_with "/info" ) } {
      set newUri [string map [list "info" "files"] [HTTP::uri]]
      HTTP::redirect "http://[HTTP::host]$newUri"
      }
      

      if you want to replace the host/port , change the redirection part from HTTP::redirect "http://[HTTP::host]$newUri" to HTTP::redirect "http://10.10.10.10:9000$newUri"

  • You can try this iRule - I've written it without the redirect as I presume you want to rewrite the header and URI on the fly - a redirect to a private IP may cause issues if accessing over the internet? Please correct me if not. I've also rewritten the host header using the selected back-end server IP and port you connect to, rather than hard code the IP addresses - this can be changed if needs be Let me know how you get on

     

    when HTTP_REQUEST {
        if {[HTTP::uri] starts_with "/info"} {
            set newUri [string map [list "info" "files"] [HTTP::uri]]
            HTTP::header replace Host "[LB::server addr]:[LB::server port]"
            HTTP::uri $newUri   
        }
    }

    Cheers Lee

     

  • I just need to redirect / rewwrite requests that come in to F5-VIP-Jenkins/info to jenkins-server:9000/jenkins_info. while still persisting any additional path that is supplied. So F5-VIP-Jenkins/info/project1 would redirect to jenkins-server:9000/jenkins_info/project1.

    just another example

    e.g.

    // config
    
    root@(ve13a)(cfg-sync In Sync)(Active)(/Common)(tmos) list ltm virtual bar
    ltm virtual bar {
        destination 172.28.24.10:80
        ip-protocol tcp
        mask 255.255.255.255
        pool foo
        profiles {
            http { }
            tcp { }
        }
        rules {
            qux
        }
        source 0.0.0.0/0
        source-address-translation {
            type automap
        }
        translate-address enabled
        translate-port enabled
        vs-index 17
    }
    root@(ve13a)(cfg-sync In Sync)(Active)(/Common)(tmos) list ltm pool foo
    ltm pool foo {
        members {
            200.200.200.101:9000 {
                address 200.200.200.101
            }
        }
    }
    root@(ve13a)(cfg-sync In Sync)(Active)(/Common)(tmos) list ltm rule qux
    ltm rule qux {
    when HTTP_REQUEST_RELEASE {
      if { [HTTP::uri] starts_with "/info/" } {
        HTTP::host "[LB::server addr]:[LB::server port]"
        HTTP::uri [string map {"/info/" "/jenkins_info/"} [HTTP::uri]]
      }
    }
    }
    
    // test
    
    [root@ve13a:Active:In Sync] config  ssldump -Aed -nni 0.0 port 80 or port 9000
    New TCP connection 1: 172.28.24.1(42574) <-> 172.28.24.10(80)
    1515777465.5177 (0.0006)  C>S
    ---------------------------------------------------------------
    GET /info/project1/helloworld HTTP/1.1
    User-Agent: curl/7.29.0
    Host: 172.28.24.10
    Accept: */*
    
    ---------------------------------------------------------------
    
    New TCP connection 2: 200.200.200.14(3889) <-> 200.200.200.101(9000)
    1515777465.5189 (0.0011)  C>S
    ---------------------------------------------------------------
    GET /jenkins_info/project1/helloworld HTTP/1.1
    User-Agent: curl/7.29.0
    Host: 200.200.200.101:9000
    Accept: */*
    
    ---------------------------------------------------------------
    
  • Check out F5's LTM > Policies instead of creating an iRule. You can do all this in the GUI.

     

    Create a new policy, name it, then create a rule within the policy "Create policy" button, do all your rules here. URI and redirect changes. Then go to your VIP, resource tab, apply the policy.