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?
- hooleylistCirrostratusYou can try something like this to redirect any request with a host header that doesn't start with www and is not an IP address to https://www< host >:
when HTTP_REQUEST { Check if host doesn't start with www and has at least one alpha character if { not ([string tolower [HTTP::host]] starts_with "www") and [string match {[a-zA-Z]} [HTTP::host]] }{ Redirect to the same host via https with www. prepended HTTP::redirect "https://www.[HTTP::host][HTTP::uri]" } }
- yihwen_100254NimbostratusHi Aaron
- hooleylistCirrostratusWhat happens when you try the iRule? Does the client get a redirect? Can you add logging to the iRule and check /var/log/ltm for the output?
when HTTP_REQUEST { log local0. "[IP::client_addr]:[TCP::client_port]: [HTTP::method] request to [HTTP::host][HTTP::uri]" Check if host doesn't start with www and has at least one alpha character if { not ([string tolower [HTTP::host]] starts_with "www") and [string match {[a-zA-Z]} [HTTP::host]] }{ Redirect to the same host via https with www. prepended HTTP::redirect "https://www.[HTTP::host][HTTP::uri]" log local0. "[IP::client_addr]:[TCP::client_port]: Redirecting to https://www.[HTTP::host][HTTP::uri]" } }
- yihwen_100254NimbostratusHere is the output
- Michael_YatesNimbostratusYou can apply this to your HTTP Virtual Server.
- hooleylistCirrostratusGood point Michael. If you want to redirect all requests to HTTPS and prepend www. to hostnames without www. you can modify it:
when HTTP_REQUEST { log local0. "[IP::client_addr]:[TCP::client_port]: [HTTP::method] request to [HTTP::host][HTTP::uri]" Check if host doesn't start with www and has at least one alpha character if { not ([string tolower [HTTP::host]] starts_with "www") and [string match {[a-zA-Z]} [HTTP::host]] }{ Redirect to the same host via https with www. prepended HTTP::redirect "https://www.[HTTP::host][HTTP::uri]" log local0. "[IP::client_addr]:[TCP::client_port]: Redirecting to https://www.[HTTP::host][HTTP::uri]" } else { Redirect to the same host via https HTTP::redirect "https://[HTTP::host][HTTP::uri]" log local0. "[IP::client_addr]:[TCP::client_port]: Redirecting to https://[HTTP::host][HTTP::uri]" } }
- Colin_Walker_12Historic F5 AccountAnd here I was just about to correct Aaron's code...that comment is confusing. Looks like the iRule is correct, regardless of the "Redirect to the same host via https" comment being copied into a confusing spot. ;)
- hooleylistCirrostratusForget spellcheck on replies--we need a QA team to review code blocks :)
- Colin_Walker_12Historic F5 AccountHah! Now you're talkin. ;)
- yihwen_100254NimbostratusHi Aaron and Michael
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