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

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

Thanks for your help! I will try it.

I try this irules, but I cannot use “string matches” in v9.x, why?

Sorry, that was a typo. It should be string match not string_match. Can you try the fixed version above?

Thanks, Aaron