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

dlogsdonmd's avatar
dlogsdonmd
Icon for Nimbostratus rankNimbostratus
Dec 22, 2014

301 Permanent HTTPS redirect

Hello,

 

I just updated an iRule for my HTTPS object as shown below, it now forwards https://cardiosmart.org to https://www.cardiosmart.org. What I need now is to modify further to incorporate a 301 permanent redirect into this same rule. I'm not sure where to include the "respond 301 location" though, would someone be able to assist?

 

Also, I do already have a "respond 301 location" included in an iRule for my HTTP object.

 

Thanks in advance!

 

Diane

 

Current iRule: when HTTP_REQUEST { if { [string tolower [HTTP::host]] contains "healthandwellness.cardiosmart.org" } { HTTP::redirect "https://www.cardiosmart.org[HTTP::uri]" } if { [string tolower [HTTP::host]] equals "cardiosmart.org" } { HTTP::redirect "https://www.cardiosmart.org[HTTP::uri]" } }

 

4 Replies

  • So, you suggest to write the rule as follows? To replace the "redirect" with "respond 301 Location" (without the quotes). Is that correct?

    Thank you.

     

    when HTTP_REQUEST { 
       if { [string tolower [HTTP::host]] contains "healthandwellness.cardiosmart.org" } {
         HTTP::redirect "https://www.cardiosmart.org[HTTP::uri]" 
       }
       if { [string tolower [HTTP::host]] equals "cardiosmart.org" } {
         HTTP::respond 301 Location "https://www.cardiosmart.org[HTTP::uri]" 
       }
    }
    

     

  • I would use and elseif and put the one that gets hit the most as the first if. That will make the rule more efficient.

     

    when HTTP_REQUEST {
        if { [string tolower [HTTP::host]] contains "healthandwellness.cardiosmart.org" } {
            HTTP::respond "HTTP/1.1 301 Moved Permanently\r\nLocation:https://www.cardiosmart.org[HTTP::uri]\r\nConnection: close\r\nContent-Length: 0\r\n\r\n"
        }
        elseif { [string tolower [HTTP::host]] equals "cardiosmart.org" } {
            HTTP::respond "HTTP/1.1 301 Moved Permanently\r\nLocation:https://www.cardiosmart.org[HTTP::uri]\r\nConnection: close\r\nContent-Length: 0\r\n\r\n"
        }
    }
    

     

  • Thanks Brad. That looks a lot more complicated. What does all that mean? Sorry, I'm IT Ops, not developer so code is a foreign language to me. Thanks for any explanation/clarification you can provide.

     

    And I believe the "healthandwellness" part would come second since I don't think that's highly used.

     

    Thanks.

     

    Diane

     

  • Another method would be to use the switch statement instead of if. If you plan to add additional redirects in the future, it may look a little cleaner...

     

    when HTTP_REQUEST { 
        switch -glob -- [string tolower [HTTP::host]] {
            "cardiosmart.org" -
            "*healthandwellness.cardiosmart.org*" {
                HTTP::respond 301 Location "XXXXX[HTTP::uri]"
                return
            }
        }
    }
    

     

    Because the code block messes up hyperlinks, replace the XXXXX with https://www.cardiosmart.org.

    In this case, the -glob says to allow wildcards in the checks, and you have two conditions (the second which uses the * wildcard) that will cause the HTTP redirect response. With this format, if you needed to add new hosts to redirect, you would just add a - at the end of the healthandwellness line and put the additional host on the next line.

    Additional documentation regarding if/elseif/else and switch statements can be found here