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

jgoetsch_236444's avatar
jgoetsch_236444
Icon for Nimbostratus rankNimbostratus
Nov 30, 2015

Prepend www, redirect to https, and preserve full URL

Hello All,

 

I would like to use an iRule that does the following type of redirect/rewrite:

 

http://testsite.com/about -> https://www.testsite.com/about

 

Ideally, if other valid urls like the below are entered, I do not want it to prepend the www:

 

http://blog.testsite.com -> https://blog.testsite.com

 

Searching around, I've found iRules that do most of what I want, but not the whole thing.

 

Regards,

 

Jason

 

2 Replies

  • Hi Jason,

    Give this a try.

    when HTTP_REQUEST {
        if { [string to lower [HTTP::host]] eq "testsite.com" } {
        HTTP::redirect https://www.[getfield [HTTP::host] ":" 1][HTTP::uri] 
    }
        else {HTTP::redirect https://[getfield [HTTP::host] ":" 1][HTTP::uri]}
    }
    
  • If you are running at least 11.4, you can also achieve this using a Local Traffic Policy

    It might look like this:

    ltm policy www-redirect-append {
        controls { forwarding }
        requires { http }
        rules {
            rule-testsite.com {
                actions {
                    0 {
                        http-reply
                        redirect
                        location "https://www.[getfield [HTTP::host] : 1][HTTP::uri]"
                    }
                }
                conditions {
                    0 {
                        http-host
                        values { testsite.com }
                    }
                }
                ordinal 1
            }
            rule-default {
                actions {
                    0 {
                        http-reply
                        redirect
                        location "https://[getfield [HTTP::host] : 1][HTTP::uri]"
                    }
                }
                ordinal 2
            }
        }
        strategy first-match
    }