Forum Discussion

johnscr_45871's avatar
johnscr_45871
Icon for Nimbostratus rankNimbostratus
Dec 12, 2008

Simple match of URL based on regular expression

Hi

 

 

I am haveing some trouble with this simple Irule and was hoping someone could help. All I have to do is check the incomming URL and see if it matches a reg ex. If it matches the reg ex, then pass the request on to "PoolB".

 

The regular expression (as one of my devs has writtin it) looks like this:

 

 

(.*)?postcode=(.*)&email=(.*)

 

 

The incomming url will look like this:

 

 

http://www.bla.com/folder1/?postcode=POSTCODE&email=EMAIL

 

where POSTCODE is an actual postcode and EMAIL is an actual email address.

 

 

I know this should be simple but I just cant get it working??

 

Any help will be met with massive amounts of karma, respect, love and chocolate covered goodies.

 

 

Thanks

3 Replies

  • Hi there,

    A string comparison would be more efficient than a regex. If you really want to check if the postcode and email parameters are in the URI in a particular order you could use [string match *postcode=*email=* [HTTP::query]] to perform the test. Else if you want to check if the query string contains the two parameters in any order you could do two checks ([string match *postcode=*email=* [HTTP::query]] or [string match *email=*postcode=* [HTTP::query]]) or parse the parameters using URI::query (Click here)

    Here is the first option:

      
      when HTTP_REQUEST {  
        
          Check if the query string contains anything postcode anything email anything or anything email anything postcode  
         if {([string match *postcode=*email=* [HTTP::query]] or [string match *email=*postcode=* [HTTP::query]])}{  
        
             Query string matched, so use PoolB  
            pool PoolB  
        
          } else {  
        
             Query string did not match, so use PoolA  
            pool PoolA  
         }  
      }  
      

    Here is the second option which parses the query string for the actual parameter values and checks if they have a length. If you don't care whether the parameters have a value, you can remove the string length check on the values:

      
      when HTTP_REQUEST {  
        
          Check if the query string contains the postcode and email email parameters with lengths  
         if {([string length [URI::query [HTTP::query] postcode]] and [string length [URI::query [HTTP::query] email]])}{  
        
             Query string matched, so use PoolB  
            pool PoolB  
        
          } else {  
        
             Query string did not match, so use PoolA  
            pool PoolA  
         }  
      }  
      

    Aaron
  • So after some persistance I got this one to work. I hope its doing the correct thing, but initial testing looks OK.

     

     

     

     

    when HTTP_REQUEST {

     

     

    if {[HTTP::uri] matches_regex "(.*)?postcode=(.*)&email=(.*)"} {

     

    log "Landing pages on Pool B"

     

    pool uSwitchPoolB

     

    } else {

     

     

    }

     

    }