Forum Discussion

yihwen_100254's avatar
yihwen_100254
Icon for Nimbostratus rankNimbostratus
Jun 14, 2011

Generic iRule to redirect domain.com to www.domain.com

Hi

 

 

I am planning to create an generic iRule to perform a http://domain.com to https://www.domain.com I know in the F5 v10.x.x, there is a _sys_https_redirect and that will actually redirect any http:// to https://

 

 

But as most users love to type http://domain.com, I am thinking of creating a generic iRule so that I can use it for all my domains. Below is the iRule to perform what I want but this is not generic.

 

 

when HTTP_REQUEST {

 

if {

 

[HTTP::host] eq "domain.com" } {

 

HTTP::redirect "https://www.domain.com[HTTP::uri]"

 

}

 

}

 

 

If I have another domain called http://abc.com, using this iRule above will not work. What is the best way of achieving this?

 

  • Can you try this instead?

    when HTTP_REQUEST {
    log local0. "[IP::client_addr]:[TCP::client_port]: [HTTP::method] to [HTTP::host][HTTP::uri]"
    switch -glob [string tolower [HTTP::host]] {
    "www.*" {
    HTTP::redirect "https://[HTTP::host][HTTP::uri]"
    log local0. "Redirecting www host to https://[HTTP::host][HTTP::uri]"
    }
    {*[a-z]*} {
    HTTP::redirect "https://www.[HTTP::host][HTTP::uri]"
    log local0. "Redirecting non-www host to www.[HTTP::host][HTTP::uri]"
    }
    "" {
    HTTP::redirect "https://[IP::local_addr][HTTP::uri]"
    log local0. "Redirecting null host to [IP::local_addr][HTTP::uri]"
    }
    default {
    HTTP::redirect "https://[HTTP::host][HTTP::uri]"
    log local0. "Redirecting IP address [HTTP::host] to https://[HTTP::host][HTTP::uri]"
    }
    }
    }

    Sample log output for each case:

    < HTTP_REQUEST >: 10.1.0.11:54026: GET to www.example.com/test
    < HTTP_REQUEST >: Redirecting www host to https://www.example.com/test
    < HTTP_REQUEST >: 10.1.0.11:54027: GET to example.com/test
    < HTTP_REQUEST >: Redirecting non-www host to www.example.com/test
    < HTTP_REQUEST >: 10.1.0.11:54028: GET to /test
    < HTTP_REQUEST >: Redirecting null host to 10.1.0.15/test
    < HTTP_REQUEST >: 10.1.0.11:54030: GET to 10.1.0.15/test
    < HTTP_REQUEST >: Redirecting IP address 10.1.0.15 to https://10.1.0.15/test

    Aaron
  • Jun 15 12:28:11 local/tmm1 info tmm1[5213]: Rule https_www_redirect : 103.63.211.158:4259: HEAD request to /

     

    Jun 15 12:28:11 local/tmm1 info tmm1[5213]: Rule https_www_redirect : 103.63.211.158:4259: Redirecting to https:///

     

    Jun 15 12:29:12 local/tmm info tmm[5212]: Rule https_www_redirect : 103.63.211.158:4796: HEAD request to /

     

    Jun 15 12:29:12 local/tmm info tmm[5212]: Rule https_www_redirect : 103.63.211.158:4796: Redirecting to https:///

     

    Jun 15 12:30:12 local/tmm1 info tmm1[5213]: Rule https_www_redirect : 103.63.211.158:1351: HEAD request to /

     

    Jun 15 12:30:12 local/tmm1 info tmm1[5213]: Rule https_www_redirect : 103.63.211.158:1351: Redirecting to https:///

     

    Jun 15 12:31:12 local/tmm info tmm[5212]: Rule https_www_redirect : 103.63.211.158:1854: HEAD request to /

     

    Jun 15 12:31:12 local/tmm info tmm[5212]: Rule https_www_redirect : 103.63.211.158:1854: Redirecting to https:///

     

    Jun 15 12:31:39 local/tmm1 info tmm1[5213]: Rule https_www_redirect : 103.63.211.158:15651: GET request to www.domain.com/test

     

    Jun 15 12:31:39 local/tmm1 info tmm1[5213]: Rule https_www_redirect : 103.63.211.158:15651: Redirecting to https://www.domain.com/test

     

    Jun 15 12:31:39 local/tmm info tmm[5212]: Rule https_www_redirect : 203.12.2.160:34898: GET request to www.domain.com/test

     

    Jun 15 12:31:39 local/tmm info tmm[5212]: Rule https_www_redirect : 203.12.2.160:34898: Redirecting to https://www.domain.com/test

     

    Jun 15 12:31:44 local/tmm1 info tmm1[5213]: Rule https_www_redirect : 103.63.211.158:15651: GET request to www.domain.com/

     

    Jun 15 12:31:44 local/tmm1 info tmm1[5213]: Rule https_www_redirect : 103.63.211.158:15651: Redirecting to https://www.domain.com/

     

    Jun 15 12:31:52 local/tmm1 info tmm1[5213]: Rule https_www_redirect : 103.63.211.158:15651: GET request to domain.com/test

     

    Jun 15 12:31:52 local/tmm1 info tmm1[5213]: Rule https_www_redirect : 103.63.211.158:15651: Redirecting to https://domain.com/test

     

    Jun 15 12:32:13 local/tmm info tmm[5212]: Rule https_www_redirect : 103.63.211.158:2378: HEAD request to /

     

    Jun 15 12:32:13 local/tmm info tmm[5212]: Rule https_www_redirect : 103.63.211.158:2378: Redirecting to https:///

     

  • Can you try testing with the latest version I just posted? I think one issue with the other versions was they handle requests with no host header properly. The latest version will redirect to the virtual server IP via HTTPS. If this isn't a routable IP for clients you could hard code the correct hostname or IP address. Of course, this would need to be customized per virtual server.

     

     

    Aaron
  • Hi Michael

     

     

    Sorry for the wrong info. I tried your code again and it works! Not sure how different Aaron code is but Aaron code does not work.

     

     

    yihwen
  • Hi

     

     

    I also check the default _sys_https_redirect iRule

     

     

    The code is

     

    when HTTP_REQUEST {

     

    set host [HTTP::host]

     

    HTTP::respond 302 Location "https://$host/"

     

    }

     

     

    Is it better to use HTTP::respond 302 Location "https://$host/"

     

    or HTTP::redirect "https://[HTTP::host][HTTP::uri]"
  • It looks like your client is making requests with no host header. If that's the case, then you couldn't redirect them using https://[HTTP::host][HTTP::uri] or you'd get an invalid redirect to https:/// like is shown in your logs. Are you sure the last example I posted doesn't work? It tested fine in per the logs I posted.

     

     

    Also, HTTP::respond 302 Location and HTTP::redirect result in the same 302 response being sent to the client. So it doesn't make much difference which method you use.

     

     

    Aaron