Forum Discussion

rdessert_76127's avatar
rdessert_76127
Icon for Nimbostratus rankNimbostratus
Feb 16, 2012

Force 301 redirect to https for multiple http URI's

I need to write an irule for use on my http virtual server that will perform a 301 redirect to https for two possible http URI's. I figured out how to do it for one URI, but am struggling with proper syntax.

 

 

URI's are /abc and /def

 

 

 

Does this look correct / best way to handle it?

 

 

 

when HTTP_REQUEST {

 

if { [string tolower [HTTP::uri]] contains "/abc" or "/def"}{

 

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

 

}

 

}

 

 

Thanks for any input!

 

 

Rich

 

 

4 Replies

  • Hi rdessert,

    Try this:

    
    when HTTP_REQUEST {
    if { [string tolower [HTTP::uri]] starts_with "/abc" || [string tolower [HTTP::uri]] starts_with "/def" } {
    HTTP::respond 301 Location "https://[getfield [HTTP::host] : "1"][HTTP::uri]"
    }
    }
    

    Hope this helps.
  • Here's another option using a switch statement:

    
    when HTTP_REQUEST {
    switch -glob [string tolower [HTTP::uri]] {
    "/abc*" -
    "/def*" { 
    HTTP::respond 301 Location "https://[HTTP::host][HTTP::uri]"
    }
    }
    }
    

    I'm assuming you want starts_with like matching so I put a wildcard on the end of the directories. If you want contains like matching, you could put an asterisk at the beginning of the directories as well.

    Aaron