Forum Discussion

Pav_70755's avatar
Pav_70755
Icon for Nimbostratus rankNimbostratus
Sep 20, 2011

HTTP Re-Direct uri replace?

I have the following rule written


when HTTP_REQUEST { 
                  if { [HTTP::uri] contains "?storyId=" } { 
                      pool STAGE_directmarketing.thomsonnet.co.uk-80 
                  } 
                  elseif { [HTTP::uri] starts_with "/win" or [HTTP::uri] starts_with "/Win" }{ 
                  HTTP::redirect "http://www.news.com/Marketing-Services/" 
                  } 
                  elseif { [HTTP::uri] ends_with "/talk" } { 
                  HTTP::redirect "http://www.news.com/Marketing-Services/" 
                  } 
                  elseif { [HTTP::uri] ends_with "/find" } { 
                  HTTP::redirect "http://www.news.com/Business-Data/" 
                  } else { 
                     pool STAGE_www.news.com-80 
         } 
} 

And what I've been asked to do is the following:

anything with "/win" in the uri to replace it with "/News-Advice/

e.g if someone has the following url bookmarked

www.news.com/win/News-Archive it should go to

www.news.com/News-Advice/News-Archive

any help would be much apprecaited.

Thanks

Pav

  • HI Brian,

    Just tried it and it works although you are right after testing more links its trimming other characters from the uri... what would be the way to do it using regsub? or defining the 3 variables win, talk and find?

    this is what I have atm now, how can I get this to work with the previous rule?

    when HTTP_REQUEST {
        if { [HTTP::uri] starts_with "/win"  } {
            set wininsert [string trimleft [HTTP::uri] /win]
            HTTP::redirect "http://www.news.com/News-Advice/$wininsert"
    }
        elseif { [HTTP::uri] starts_with "/Win"  } {
            set wininsert [string trimleft [HTTP::uri] /Win]
            HTTP::redirect "http://www.news.com/News-Advice/$wininsert"
        }
        elseif { [HTTP::uri] starts_with "/talk"  } {
            set talkinsert [string trimleft [HTTP::uri] /talk]
            HTTP::redirect "http://www.news.com/Marketing-Services/$talkinsert"
    }
        elseif { [HTTP::uri] starts_with "/Talk"  } {
            set talkinsert [string trimleft [HTTP::uri] /Talk]
            HTTP::redirect "http://www.news.com/Marketing-Services/$talkinsert"
        }
         elseif { [HTTP::uri] starts_with "/find"  } {
            set findinsert [string trimleft [HTTP::uri] /find]
            HTTP::redirect "http://www.news.com/Business-Data/$findinsert"
    }
         elseif { [HTTP::uri] starts_with "/Find"  } {
            set findinsert [string trimleft [HTTP::uri] /Find]
            HTTP::redirect "http://www.news.com/Business-Data/$findinsert"
        }
        else {
             pool STAGE_www.news.comk-80 
        }  
    }
     
  • Posted By Brian on 09/21/2011 07:02 AM

    I tested this one and it works...maybe someone can come up with something better:

    set wininsert [string range [HTTP::uri] 4 end]
    set talkinsert [string range [HTTP::uri] 5 end]
    set findinsert [string range [HTTP::uri] 5 end]
    

    Hi Brian,

    I've just tried it using this method and it works perfectly thankyou for all your help, how is this rule doing the insert then with the different numbers, it would be good to understand how the Irule works fully!

    Pav

  • The range command creates a new string of the URI minus the first 4 or 5 characters.

     

     

    Not sure if you saw my comments from the very beginning, but you should probably get rid of the else statement and put that pool as the default pool for the virtual server.
  • Posted By Brian on 09/21/2011 07:26 AM

    The range command creates a new string of the URI minus the first 4 or 5 characters.

    Not sure if you saw my comments from the very beginning, but you should probably get rid of the else statement and put that pool as the default pool for the virtual server.

    Thanks for all the help Brian, I will remove the else and pool statement I also have one last question is there a way to make the re-directs permanement 301 re-directs using

    HTTP::respond 301 "location"

  •  elseif { [HTTP::uri] starts_with "/win" } {
            set wininsert [string range [HTTP::uri] 4 end]
            HTTP::respond 301 "Location" "http://www.news.com/News-Advice$wininsert"

    Im using that and it works but it starts with a 301 and ends with a 302 is there a way to permanentley keep it as a 301?
  • Here's an option which expands on Brian's examples. The iRule cannot send a 302. So if you're seeing a 302 at the client, it's most likely coming from the web application.

    
    when HTTP_REQUEST {
    switch -glob [string tolower [HTTP::uri]] {
    "/win*" {
    HTTP::respond 301 Location "http://www.news.com/News-Advice/[string range [HTTP::uri] 4 end]"
    }
    "/talk*" {
    HTTP::respond 301 Location "http://www.news.com/Marketing-Services/[string range [HTTP::uri] 5 end]"
    }
    "/find*" {
    HTTP::respond 301 Location "http://www.news.com/Business-Data/[string range [HTTP::uri] 5 end]"
    }
    default {
     pool STAGE_www.news.comk-80 
    }
    }
    }
    

    Aaron
  • Thanks Aaron I've managed to get the rule working as follows

     when HTTP_REQUEST {
        if { [HTTP::uri] starts_with "/win" or [HTTP::uri] starts_with "/Win" } {
            set wininsert [string range [HTTP::uri] 4 end]
            HTTP::respond 301 "location" "http://www.news.com/News-Advice$wininsert"
    }
    elseif { [HTTP::uri] starts_with "/talk" or [HTTP::uri] starts_with "/Talk"  } {
            set talkinsert [string range [HTTP::uri] 5 end]
            HTTP::respond 301 "Location" "http://www.news.com/Marketing-Services$talkinsert"
        }
         elseif { [HTTP::uri] starts_with "/find" or [HTTP::uri] starts_with "/Find"  } {
            set findinsert [string range [HTTP::uri] 5 end]
            HTTP::respond 301 "Location" "http://www.news.com/Business-Data$findinsert"
    }
         elseif { [HTTP::uri] contains "2fwusjt568" } {
            pool STAGE_www.news.com-80 
        }  
    }
    
    
    when HTTP_RESPONSE {
     if { [HTTP::status] == 302 } {
            HTTP::respond 301 Location [HTTP::header Location] 
     }
    }

    However there is a certain page which is the uri that contains "2fwusjt568" that doesnt work with the forced 301 re-direction, how can I omit this page from the if { [HTTP::status] == 302 } { part of the rule?

    Thanks

    Pav
  • I don't think you'd want to rewrite all 302s to 301s as that tells the client and any intermediate proxies to permanently cache the redirect.

    If you want to skip just Locations which contain 2fwusjt568 you could use something like this:

    
    when HTTP_RESPONSE {
        if { [HTTP::status] == 302  and not ([HTTP::header Location] contains "2fwusjt568")} {
          HTTP::respond 301 Location [HTTP::header Location] 
       }
    }
    

    Aaron