Forum Discussion

raytoles_75680's avatar
raytoles_75680
Icon for Nimbostratus rankNimbostratus
Nov 08, 2009

Redirects for old site - iRule Newbie needs help

We have to support some redirects for old sites after our one of our new sites goes live. Our plan is the change our dns such the the old site request are sent to our new F5 virtual server. But I received a new requirement which requires a redirect for existing product items.

Requirements:

http://oldsite..com/itemnumber.html redirect http://newsite.org/scrubs/imagine/itemnumber.aspx

and

http://oldsite.com redirect to http://newsite.org/scrubs/imagine

Here's what I have, and it does not work:

  
  when HTTP_REQUEST {  
     switch -glob [string tolower [HTTP::host]] {  
        "oldsite.com" {  
             if a uri does not exist  
            if { [HTTP::uri] equals "/" } {  
  log local0. "URI is empty, redirect to /scrubs/imagine"    
               HTTP::redirect "http://newsite.org/scrubs/imagine"  
             if a uri exist     
            } else {  
  log local0. "URI exists, redirect to /scrubs/imagine/uri"      
               HTTP::redirect "http://newsite.org/scrubs/imagine/[HTTP::uri]"  
               }  
              
         }  
     }  
  }  
    
  
  • Try it this way

      
     when HTTP_REQUEST {  
      if {[string tolower [HTTP::host]] eq "oldsite.com" } {  
       switch -glob [HTTP::uri] {  
         "/" {  
          log local0. "redirect on empty URI"  
          HTTP::redirect "http://newsite.org/scrubs/imagine "  
      }  
         "/itemnumber.html" {  
          log local0. "redirect on itemnumber"  
          HTTP::redirect "http://newsite.org/scrubs/imagine/itemnumber.aspx"  
                            }  
       }  
      }  
     }  
     

    I hope this helps

    CB

  • Thanks CB. We have about 50+ item numbers to support. So "itemnumber".html can be 400x, 401x, and so forth. We want to redirect the to the corresponding "itemnumber".aspx if it exist in the request.

     

     

    http://oldsite.com/itemnumber.html redirect to http://newsite.org/scrubs/imagine/sameitemnumber.aspx.

     

     

    Requests for the root of the oldsite should go to the root of the new site.

     

     

    http://oldsite.com/ redirect to http://newsite.org/scrubs/imagine

     

     

    Sorry I was not clear on this. I appreciate it.
  • So what you are saying is you can have the following:

     

     

     

    http://oldsite.com/400x.html to http://newsite.org/scrubs/imagine/400x.aspx

     

     

     

    CB

     

  • How about this

      
      when HTTP_REQUEST {    
        if {[string tolower [HTTP::host]] eq "oldsite.com" } {    
         switch -glob [HTTP::uri] {    
           "/" {    
            log local0. "redirect on empty URI"    
            HTTP::redirect "http://newsite.org/scrubs/imagine "    
        }    
           default {    
           set base [URI::basename [HTTP::uri]]  
           log local0. "This is the base variable $base"  
           set sbase [split $base .]  
           log local0. "this is the base split by the . : $sbase"  
           set itemn [lindex $sbase 0]  
            log local0. "redirect on itemnumber without the extension: $itemn"    
            HTTP::redirect "http://newsite.org/scrubs/imagine/$itemn.aspx"    
                              }    
         }    
        }    
       }    
      

    This is also making an assumption that you will either have no URI or a uri with a valid . at the end of the URI.

    CB

  • You can try combining the elements

    For example

    set itemn [lindex [split [URI::basename [HTTP::uri]] . ] 0 ]

    This would eliminate 3 "set" statements

    Example:

      
       when HTTP_REQUEST {      
          if {[string tolower [HTTP::host]] eq "oldsite.com" } {      
           switch -glob [HTTP::uri] {      
             "/" {      
              log local0. "redirect on empty URI"      
              HTTP::redirect "http://newsite.org/scrubs/imagine "      
          }      
             default {      
              set itemn [lindex [split [URI::basename [HTTP::uri]] . ] 0 ]   
              log local0. "redirect on itemnumber without the extension: $itemn"      
              HTTP::redirect "http://newsite.org/scrubs/imagine/$itemn.aspx"      
                        }      
           }      
          }      
      }      
      

    CB

  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus
    You could also use URI::query [HTTP::uri] "parameter_name" (Click here) to retrieve the value of a query string parameter named parameter_name.

     

     

    Aaron