For more information regarding the security incident at F5, the actions we are taking to address it, and our ongoing efforts to protect our customers, click here.

Forum Discussion

sstafford's avatar
sstafford
Icon for Nimbostratus rankNimbostratus
Jul 30, 2008

string based https redirect

Hi all--yes I'm new.

 

 

Have a customer that needs a specific page or two forced to https when requested by a client. After poking around some, I came up with

 

 

when HTTP_REQUEST {

 

 

Check for given string and redirect if found

 

if {[HTTP::uri] contains "alumni/giving/gift/"}{

 

 

HTTP::respond 301 Location "https://[getfield [HTTP::host] : 1][HTTP::uri]"

 

 

}

 

 

 

}

 

 

which I think will work, at least for the string above. I also assume there's some sort of "logical or" that I can add so that multiple strings can be controlled by a single iRule?

 

 

Thanks

6 Replies

  • Ok, I've updated after more reading--also, I've switched to the "ends_with" operator. Will this work?

     

     

    when HTTP_REQUEST {

     

     

    Check for given string and redirect if found

     

    if {[HTTP::uri] ends_with "alumni/giving/gift/"}{

     

     

    HTTP::respond 301 Location "https://[getfield [HTTP::host] : 1][HTTP::uri]"

     

     

    } elseif { [HTTP::uri] ends_with "alumni/giving/pledge/" } {

     

     

    HTTP::respond 301 Location "https://[getfield [HTTP::host] : 1][HTTP::uri]"

     

     

    } elseif { [HTTP::uri] ends_with "alumni/directory/search.aspx" } {

     

     

    HTTP::respond 301 Location "https://[getfield [HTTP::host] : 1][HTTP::uri]"

     

     

    } elseif { [HTTP::uri] ends_with "alumni/directory/update.aspx" } {

     

     

    HTTP::respond 301 Location "https://[getfield [HTTP::host] : 1][HTTP::uri]"

     

     

    }

     

     

     

     

  • Patrick_Chang_7's avatar
    Patrick_Chang_7
    Historic F5 Account
    You are missing a closing } in the iRule, but other than that, I believe it will do what you want.
  • Here is another way to go about it with a wildcard switch and it will perform a bit better than all of the comparisons above:

    when HTTP_REQUEST { 
       switch -glob [string tolower [HTTP::uri]] { 
         "*alumni/giving/gift/" - 
         "*alumni/giving/pledge/" - 
         "*alumni/directory/search.aspx" - 
         "*alumni/directory/update.aspx" { 
           HTTP::respond 301 Location "https://[getfield [HTTP::host] : 1][HTTP::uri]"  
         } 
       } 
     }

    I also threw in a "string tolower" in the comparison string so that you would catch all case options (since you seem to be using IIS and IIS is case-insensitive).

    -Joe
  • Ok, so the client has come back to me, as clients are wont to do, and said. "This is great, but what I really need is for everything other than those four pages to be forced to http."

    So, I came up with this rule, to be implented on the https virtual server.

     
      
     when HTTP_REQUEST {  
        switch -glob [string tolower [HTTP::uri]] {  
          "*alumni/giving/gift/" -  
          "*alumni/giving/pledge/" -  
          "*alumni/directory/search.aspx" -  
          "*alumni/directory/update.aspx" {  
           don't do anything... 
          }  
        }  
      
        default { 
            HTTP::respond 301 Location "http://[getfield [HTTP::host] : 1][HTTP::uri]"   
        } 
      } 
      
     

    Any comments?
  • Ah...Rejected by the server.

    This works better...

     
     when HTTP_REQUEST {  
        switch -glob [string tolower [HTTP::uri]] {  
          "*alumni/giving/gift/" -  
          "*alumni/giving/pledge/" -  
          "*alumni/directory/search.aspx" -  
          "*alumni/directory/update.aspx" {  
           don't do anything... 
          }  
        default { 
           HTTP::respond 301 Location "http://[getfield [HTTP::host] : 1][HTTP::uri]"   
          } 
        }  
      }