Forum Discussion

Brett__01_13258's avatar
Brett__01_13258
Icon for Nimbostratus rankNimbostratus
Mar 14, 2014

irule redirect on uri including default location

Im having trouble with requests that i need to redirect based on uri. My irule is

 

when HTTP_REQUEST { switch -glob [string tolower [HTTP::uri]] { "/test/" - "/test1/" { HTTP::redirect "http://redirected.server.com[HTTP::uri]" } default { pool POOL_MainPool_Server-80 } } }

 

I want to have any request that is for www.request.com/test to go to second server.

 

It works fine for any request that includes the / eg: www.request.com/test/ but not without the / If i remove the / from my irule eg "/test" then any request such as www.request.com/testing also redirects but I dont want it to.

 

Also I dont want it picking up on the secondary level eg /Firstlevel/test/ I want the exact match of /test/ but include if the request is just default page /test

 

Does anyone now how I would go about this?

 

Thanks in advance Brett

 

  • Hi Brett!

     

    If you use /test as an option "/testing" it would not trigger it. Only starts_with would.

     

    /Patrik

     

    Ps. You might want to remove the "-glob" since it forces the LTM to work with single core while processing traffic on VIP's that has this iRule assosiated with them (unless you need wild card matching, which you don't in your example. Ds.

     

  • Brett,

    Something like this should work by catching /test, or anything after it:

    when HTTP_REQUEST {
     switch -glob [string tolower [HTTP::uri]] {
      "/test*" -
      "/test1/*" { HTTP::redirect "http://redirected.server.com[HTTP::uri]" }
      default { pool POOL_MainPool_Server-80 } 
      } 
      }
    
    • Brett__01_13258's avatar
      Brett__01_13258
      Icon for Nimbostratus rankNimbostratus
      Hi Cory thanks for the reply THe issue with the rule above is it will also catching /testing for example What i need it to do is only redirect /test and /test/whatever but not anything after /test such as /testing I may have to include double rules?
  • Hi Brett!

    Depending on how the rest of your iRule looks like and how many cases you have in the switch personally I'd probably go for a class search.

    when HTTP_REQUEST {
    
        set uri [string tolower [HTTP::uri]]
    
        if { [class search test_redirects starts_with $uri] or $uri equals "/test/" } {
            HTTP::redirect "http://redirected.server.com[HTTP::uri]"        
        }
    }
    

    "test_redirects" is a data group list of the type string and would contain "/test/", "/test1/", etc. The iRule above keeps the CMP intact and saves you some resources.

    You could also use a data grouplist for the URI's without trailing slash, but if you only have the one, "/test", I'd keep "or" instead, like the example above.

    Good luck!

    /Patrik

  • Thanks Patrik that makes sense in keeping seperate data group list.

     

    I have tried and works fine Cheers Brett