Forum Discussion

cymru81's avatar
cymru81
Icon for Altocumulus rankAltocumulus
Sep 03, 2019

irule help based on uri

Hi, ive done some searching on here but cannot find the exact help i need. We have the following scenario, can someone assist with an irule that will achieve this please?

 

we have one VIP that hostname site.abc.com resolves to, if user visits https://site.abc.com/live it should redirect to pool "pool_live" maintaining the uri "/live". If user visits https://site.abc.com/test it should redirect to pool "pool_test" again maintaining the uri.

 

how can we achieve this?

 

thanks in advance!

7 Replies

  • Hi

    You could do something like

    when HTTP_REQUEST {
    	if {[HTTP::uri] starts_with "/live"}{
    		pool pool_live
    	} elseif {[HTTP::uri] starts_with "/test"}{
    		pool pool_test
    	}
    }
    • DanS92's avatar
      DanS92
      Icon for Cirrus rankCirrus

      Another way would be with a Local Traffic Policy instead of an iRule. Under Local Traffic > Policies > Policy List, create a new policy. You'd need a rule in the policy like this for the first request:

       

      The rule for the second URI would be built the same way. Then apply the policy to the VIP.

    • cymru81's avatar
      cymru81
      Icon for Altocumulus rankAltocumulus

      Im sorry to move the goalposts but my brief has changed to the following now:

       

      we have one VIP that hostname site.abc.com resolves to, if user visits https://site.abc.com/live it should redirect to pool "pool_live" changing the ur toi "/live_site". If user visits https://site.abc.com/test it should redirect to pool "pool_test" changing the uri to /test_site.

       

      Is this achievable?

      • Hi cymru81,

        iRule:

        when HTTP_REQUEST {
        	if { [string tolower [HTTP::host]] equals "site.abc.com" } {
        		switch -glob [string tolower [HTTP::uri]] {
        			"/live" {
        				HTTP::uri "/live_site"
        				pool pool_live
        			}
        			"/test" {
        				HTTP::uri "/test_site"
        				pool pool_test
        			}
        		}
        	}
        }

        iRule with redirect:

        when HTTP_REQUEST {
        	if { [string tolower [HTTP::host]] equals "site.abc.com" } {
        		switch -glob [string tolower [HTTP::uri]] {
        			"/live" {
        				HTTP::redirect "https://site.abc.com/live_site"
        				pool pool_live
        			}
        			"/test" {
        				HTTP::redirect "https://site.abc.com/test_site"
        				pool pool_test
        			}
        		}
        	}
        }

         If the HTTP::host is not site.abc.com, you can reject requests.

  • thank you the first rule is just the job!

     

    ive got another issue now, it works as expected so if a user goes to https://site.abc.com/live they end up at https://site.abc.com/live_site but if they try to navigate any subsequent links it fails.

     

    Basically we have an external url https://site.abc.com/live_site/ that we want a proxy mapping that will go to http://internalserver:1234/live - we have a pool "pool_live" that contains "internalserver:1234".

     

    is this totally achievable or am i missing something fundamental!

  • Hi

    To keep the rest of the path in place, use something like this. This will replace /live with /live_site and then remove the first 5 characters from the uri (so /live) and then insert the rest of the path

    when HTTP_REQUEST {
    	if {[HTTP::uri] starts_with "/live"}{
    		HTTP::uri /live_site[string range [HTTP::uri] 5 end]
    		pool pool_live
    	} elseif {[HTTP::uri] starts_with "/test"}{
    		HTTP::uri /test_site[string range [HTTP::uri] 5 end]
    		pool pool_test
    	}
    }
  • thank you once again! ive managed to get this working now with a local traffic policy and a rewrite profile, think some luck involved too!