Forum Discussion

Mike_P_'s avatar
Mike_P_
Icon for Nimbostratus rankNimbostratus
Nov 05, 2015

Need help with an irule to redirect with exceptions for certain pages

First time posting here and need some assistance. My company is requesting an irule to redirect certain websites to another site but NOT other portions. Here is an example:

 

Let's say a user points to http://www.mysite.com redirect them to http://www.myothersite.com/mysite however DON'T redirect them if they go to http://www.mysite.com/specificpage.aspx and they have, in some cases, a site with up to 20 different pages that are exceptions to this redirect.

 

I have this irule and can get the first exception to work but when I try to add more it seems to only work on the first match. Also since I have, as mentioned, 20+ matches in some cases, might it be better to do some kind of table lookup? But I am not clear on how to do that. Any help is appreciated. Here is what I have so far that works, but only with 1 exception so far (no matter how many if/or statements I've added):

 

when HTTP_REQUEST { log local0. "in HTTP_REQUEST" if {[HTTP::uri] equals "www.mysite.com/clocks.asp"}{ if {[HTTP::uri] equals "www.mysite.com/images/*"} { <----this is not matching, for example

 

pool www-mysite_pool } else { HTTP::respond 301 location "" } }

 

ANY help at all is appreciated. I am a networking guy with little programming experience and am surprised I even got a simple redirect to work to be honest but they are pounding hard for me to get this to work. Thanks!

 

5 Replies

  • R_Marc's avatar
    R_Marc
    Icon for Nimbostratus rankNimbostratus

    There are a few ways to do this. The easiest way is with a case statement:

     when HTTP_REQUEST {
        switch -glob [HTTP::uri] {
            "/clocks.asp*" {
                do stuff
            }
            "/mysite.aspx*" {
                do other stuff
            }
            default {
                non-special case stuff
            }
        }
     }
     

    If this is something that would grow and be a long standing need for additions, I would advocate something slightly different (utilizing data groups).

  • some changes of irule provided by R Marc...

    the - allow to do same action for multiple paths.

    when HTTP_REQUEST {
        switch -glob [HTTP::path] {
            "/clocks.asp" -
            "/mysite.aspx" -
            "/images/*" {
                pool www-mysite_pool
            }
            default {
                HTTP::respond 301 location "http://www.myothersite/mysite.aspx"
            }
        }
    }
    
  • Both of these are great. Much appreciated. I should get an opportunity to test soon and will update back. Thanks to both of you!

     

  • Below is the code that is working perfectly for one of the sites, however for some reason it won't work when I apply it to another site that has about 8 lines of matches, any thoughts?

     

    when HTTP_REQUEST { switch -glob [string tolower [HTTP::path]] { "/clocks.asp" - "/includes/" - "/favicon.ico" - "/images/" { pool www-distribution_pool } default { HTTP::respond 301 location "http://www.mysite.com/Distribution.aspx" } } }

     

    Here is the longer match code that does not seem to work (it just redirects everything no matter what I do):

     

    when HTTP_REQUEST {

     

    switch -glob [string tolower [HTTP::path]] { "/news/" - "/favicon/" - "/links/" - "/downloads/" - "/offices/" - "/programs/" - "/selling/" - "/wear/" - "/links/" - "/images/" { pool www-landing_pool } default { HTTP::respond 301 location "http://www.myothersite.com/landing.aspx" } } }

     

    Lastly, would it be better to use a datagroup for a longer list like that? I tried to do so but could not get that to work either. As before, ANY help appreciated!

     

  • R_Marc's avatar
    R_Marc
    Icon for Nimbostratus rankNimbostratus

    I would do it like this (myself)

    switch -glob [string tolower [HTTP::path]] {
            "/news*" -
            "/favicon.ico" -
            "/links*" -
            "/downloads*" -
            "/offices*" -
            "/programs*" -
            "/selling*" -
            "/wear*" -
            "/links*" -
            "/images*" {
                    pool www-landing_pool
            }
            default {
                    HTTP::respond 301 location "http://www.myothersite.com/landing.aspx"
            }
    }
    

    Doing it with a data-group would be much different. Don't have time to address that presently, however. There might be an example in the wiki.