Forum Discussion
yihwen_100254
Jun 14, 2011Nimbostratus
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?
- hooleylistCirrostratusCan 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]" } } }
< 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
- yihwen_100254NimbostratusJun 15 12:28:11 local/tmm1 info tmm1[5213]: Rule https_www_redirect : 103.63.211.158:4259: HEAD request to /
- hooleylistCirrostratusCan 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.
- yihwen_100254NimbostratusHi Michael
- yihwen_100254NimbostratusHi
- hooleylistCirrostratusIt 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.
Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects