Forum Discussion

Benjamin_9120's avatar
Benjamin_9120
Icon for Nimbostratus rankNimbostratus
May 30, 2010

about regular expressions

Customer will access follow uri:

 

http://www.test.com/server1

 

http://www.test.com/server2

 

......

 

http://www.test.com/serverN

 

 

I want redirect to relate resource base on server1/server2/.../serverN, how can I write this irules?

 

 

such as http://www.poc.com/server1 or pool pool_server1.

 

  • Hi Benjamin,

     

    When you match up the URI "ServerN" where exactly do you want to redirect to? Do you want to direct it to a pool based on the uri then you could do the following

     

     

    
    when HTTP_REQUEST {
       set poolname [HTTP::uri]
       if { ([HTTP::host] eq "www.test.com") and ([HTTP::uri starts_with "/server" } {
          pool pool_$poolname
          }
    }
    

     

     

    NOTE: This is untested code

     

     

    I hope this helps

     

    Bhattman

     

  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus
    Hey CB,

    I think you'd want to strip the leading forward slash from the URI. Also, you might want to take just the server to form the pool name:

    when HTTP_REQUEST {
        Check if Host is www.test.com and URI starts with /server
       if { ([HTTP::host] eq "www.test.com") and ([HTTP::uri] matches_glob {/server[0-9]*}) } {
          log local0. "[IP::client_addr]:[TCP::client_port]: Parsed [string range [HTTP::uri] 1 7] from [HTTP::uri]"
           Select the pool, taking just server from the URI
          pool pool_[string range [HTTP::uri] 1 7]
       }
    }

    Or if you're not on 10.x and can't use matches_glob, you can use string match:

    when HTTP_REQUEST {
        Check if Host is www.test.com and URI starts with /server
       if { ([HTTP::host] eq "www.test.com") and ([string match {/server[0-9]*} [HTTP::uri]]) } {
          log local0. "[IP::client_addr]:[TCP::client_port]: Parsed [string range [HTTP::uri] 1 7] from [HTTP::uri]"
           Select the pool, taking just server from the URI
          pool pool_[string range [HTTP::uri] 1 7]
       }
    }

    To do a redirect, you could replace the pool command with

    HTTP::redirect "http ://www.example.com[HTTP::uri]"

    or maybe something like

    HTTP::redirect "http ://www.example.com[string range [HTTP::uri] 0 7]"

    Aaron
  • You are right....That is what happens when you have sleep deprivation.

     

     

    Bhattman

     

  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus
    Sorry, that was a typo. It should be string match not string_match. Can you try the fixed version above?

     

     

    Thanks, Aaron