Forum Discussion

Joe_Pipitone's avatar
Joe_Pipitone
Icon for Nimbostratus rankNimbostratus
Mar 16, 2010

Rewriting - syntax

I want to be able to catch http://www.domainname.com/anyurl/anypage.aspx or http://domainname.com/anyurl/anypage.aspx- both with and without the www and rewrite as needed. I am currently using an iRule now which works fine, however I've added another if statement.

I am simply rewriting to another domain while still keeping the uri intact.

Can anyone tell me if this syntax is correct, or should I be using else if, rather than the 2nd if?

 
 when HTTP_REQUEST {  
     if { ([HTTP::host] eq "www.domainname.com") } {  
         switch -glob [HTTP::host] {  
              "/" { HTTP::redirect "http://anotherdomain.com" }  
              default {  HTTP::redirect "http://anotherdomain.com[HTTP::uri]" } 
         }  
     }  
     if { ([HTTP::host] eq "domainname.com") } {  
         switch -glob [HTTP::host] {  
              "/" { HTTP::redirect "http://anotherdomain.com" }  
              default {  HTTP::redirect "http://anotherdomain.com[HTTP::uri]" } 
         }  
     }  
 } 
 
  • Hamish's avatar
    Hamish
    Icon for Cirrocumulus rankCirrocumulus
    For performance reasons I'd suggest an else in there... However you may also want to investigate using a switch statement, or using a class (DataGroup) and the findclass/class commands to lookup the recieved host and return the redirection host (The findclass should scale better, and mean you don't have to re-write your iRule whenever you add a new host).

    e.g. (Without checking for syntax, this is for v10)

      
      when HTTP_REQUEST {   
        set redirecthost [class search -value "theclass" equals [HTTP::host]]   
        if { $redirecthost ne "" } {  
          HTTP::redirect $redirecthost  
        }  
      }  
      

    You can add the embelishments...
  • We are running v 9.4.7.

     

     

    I will modify the iRule to use if and elseif - would this show us the best performance?
  • This triggers an error - forgive me, I'm not too familiar with syntax:

    01070151:3: Rule [cplanning-rewrite] error:  
     line 8: [undefined procedure: elseif] [elseif { ([HTTP::host] eq "ourdomain.com") } {  
     switch -glob [HTTP::host] {  
     "/" { HTTP::redirect "http://anotherdomain.com" }  
     default { HTTP::redirect "http://anotherdomain.com[HTTP::uri]" } 
     }  
     } ] 
     

    The iRule:

     
     when HTTP_REQUEST {  
         if { ([HTTP::host] eq "www.ourdomain.com") } {  
             switch -glob [HTTP::host] {  
                  "/" { HTTP::redirect "http://anotherdomain.com" }  
                  default {  HTTP::redirect "http://anotherdomain.com[HTTP::uri]" } 
             }  
         }  
         elseif { ([HTTP::host] eq "ourdomain.com") } {  
             switch -glob [HTTP::host] {  
                  "/" { HTTP::redirect "http://anotherdomain.com" }  
                  default {  HTTP::redirect "http://anotherdomain.com[HTTP::uri]" } 
             }  
         }  
     } 
     
  • Hi Joe,

    I think the use of the syntax might be a bit off

    Here is what I think it might look like

       
       when HTTP_REQUEST {   
           switch -glob [HTTP::host] {   
               "ourdomain.com" -    
               "www.ourdomain.com" {   
                      HTTP::redirect "http://anotherdomain.com[HTTP::uri]"   
                                                         }   
           }   
       }  
       

    I hope this helps

    Bhattman
  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus
    Hi Joe,

    I think this consolidated version should do the same:

      
      when HTTP_REQUEST {    
         switch [string tolower [HTTP::host]] {    
            "ourdomain.com" -     
            "www.ourdomain.com" {    
               "http://anotherdomain.com[HTTP::uri]"  
            }  
         }    
      }   
      

    http://www.tcl.tk/man/tcl8.4/TclCmd/switch.htm

    If a body is specified as - it means that the body for the next pattern should also be used as the body for this pattern (if the next pattern also has a body of - then the body after that is used, and so on). This feature makes it possible to share a single body among several patterns.

    Aaron