Forum Discussion

jpvrenen_46954's avatar
jpvrenen_46954
Icon for Nimbostratus rankNimbostratus
Mar 28, 2009

Redirect using regular expressions

Hi all,

Just looking for some feedback on my iRule, it is working but would like to hear if it looks ok like this or if it can be written in a better/easier way using coding I am not aware of.

What I am trying to do is this:

redirect http://www.myserver.nl/find/some/*_Server1_*/Beer/now.html

to http://www.heineken.nl/want/Beer/now.html

Were http://www.heineken.nl/want is fixed just add Beer/now.html

&

redirect http://www.myserver.nl/find/some/*_Server2_*/Beer/now.html

to http://www.grolsch.nl/want/Beer/now.html

Were http://www.grolsch.nl/want is fixed just add Beer/now.html

So basically anything before and after _[Ss]erver[12]_ could be any character and also anything behind _[Ss]erver[12]_*/ could be anything.

To clarify we could have for example

http://www.myserver.nl/find/some/what_ever_Server1_now_what/Beer/or/Car/melike.html

then the outcome would be:

http://www.heineken.nl/want/Beer/or/Car/melike.html

I came up with the following iRule:

 
 when HTTP_REQUEST { 
 if { [HTTP::uri] starts_with "/find/some/"}{ 
 set uri [HTTP::uri] 
 log local0. "Original uri is => $uri" 
 switch -glob [HTTP::uri] { 
 "*_[Ss]erver1_*/*" { 
 regsub {/find/some/.*_[Ss]erver1_.[^/]*/} $uri "" uri 
 log local0. "Modified uri is => $uri" 
 HTTP::redirect "http://www.heineken.nl/want/$uri" 
 } 
 "*_[Ss]erver2_*/*" { 
 regsub {/find/some/.*_[Ss]erver2_.[^/]*/} $uri "" uri 
 log local0. "Modified uri is => $uri" 
 HTTP::redirect "http://www.grolsch.nl/want/$uri" 
 } 
 } 
 } 
 else {  
 pool NoBeer-80 
 } 
 } 
 

Thanks for reading!

Jeroen

1 Reply

  • Hi Jeroen,

     

     

    I think that's about as good as you can get it. You could potentially use a few string commands to find the start of /Beer/now.html in /find/some/*_Server1_*/Beer/now.html, but I think it would be overly complicated.

     

     

    If you do stick with the regex, you could update the regex to /find/some/.*_[Ss]erver1_.*?/ as .*?/ will match anything up to the first /.

     

     

    Also, you could get rid of the intermediate variable:

     

     

    HTTP::redirect "http://www.heineken.nl/want/[regsub {/find/some/.*_[Ss]erver1_.*?/} $uri ""]"

     

     

    Aaron