Forum Discussion
yihwen_100254 Nimbostratus
Nimbostratus
Jun 14, 2011Generic 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?
16 Replies
- hoolioCirrostratus You 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]" } }
 Aaron
- yihwen_100254Nimbostratus Hi Aaron
 Thanks for your quote. I have tried but it is still not working.
 Thanks
- hoolioCirrostratus What 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]" } }
 Aaron
- yihwen_100254Nimbostratus Here is the output
 Jun 14 16:25:21 local/tmm info tmm[5212]: Rule https_www_redirect : 103.63.211.158:2172: HEAD request to /
 Jun 14 16:25:54 local/tmm info tmm[5212]: Rule https_www_redirect : 103.63.211.158:27748: GET request to domain.com/
 Jun 14 16:26:21 local/tmm1 info tmm1[5213]: Rule https_www_redirect : 103.63.211.158:2699: HEAD request to /
- Michael_YatesNimbostratus You can apply this to your HTTP Virtual Server.
 It will verify if the incoming HTTP::host starts with www and redirect to HTTPS based on the initial compare:
 when HTTP_REQUEST {
 if {[HTTP::host] starts_with "www."} {
 HTTP::redirect "https://[HTTP::host][HTTP::uri]"
 } else {
 HTTP::redirect "https://www.[HTTP::host][HTTP::uri]"
 }
 }
- hoolioCirrostratus Good 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]" } }
 Aaron
- 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. ;)
 Colin
- hoolioCirrostratus Forget spellcheck on replies--we need a QA team to review code blocks :)
 Aaron
- Colin_Walker_12Historic F5 AccountHah! Now you're talkin. ;)
 Colin
- yihwen_100254Nimbostratus Hi Aaron and Michael
 Thanks for your code. I have tried both your codes but still not working. Not sure what is going on?
 Jun 15 11:20:38 local/tmm info tmm[5212]: Rule https_www_redirect : 103.63.211.158:3462: HEAD request to /
 Jun 15 11:21:39 local/tmm info tmm[5212]: Rule https_www_redirect : 103.63.211.158:3986: HEAD request to /
 Jun 15 11:22:39 local/tmm1 info tmm1[5213]: Rule https_www_redirect : 103.63.211.158:4585: HEAD request to /
 Jun 15 11:23:40 local/tmm1 info tmm1[5213]: Rule https_www_redirect : 103.63.211.158:1255: HEAD request to /
 Jun 15 11:24:40 local/tmm1 info tmm1[5213]: Rule https_www_redirect : 103.63.211.158:1821: HEAD request to /
 Jun 15 11:25:41 local/tmm info tmm[5212]: Rule https_www_redirect : 103.63.211.158:2410: HEAD request to /
 Jun 15 11:26:41 local/tmm info tmm[5212]: Rule https_www_redirect : 103.63.211.158:2936: HEAD request to /
 Jun 15 11:27:41 local/tmm info tmm[5212]: Rule https_www_redirect : 103.63.211.158:3472: HEAD request to /
 Jun 15 11:28:42 local/tmm info tmm[5212]: Rule https_www_redirect : 103.63.211.158:4036: HEAD request to /
 Jun 15 11:29:43 local/tmm info tmm[5212]: Rule https_www_redirect : 103.63.211.158:4586: HEAD request to /
 Jun 15 11:30:41 local/tmm info tmm[5212]: Rule https_www_redirect : 103.63.211.158:23950: GET request to domain.com/
 Jun 15 11:30:41 local/tmm info tmm[5212]: Rule https_www_redirect : 103.63.211.158:23950: Redirecting to https://domain.com/
 Jun 15 11:30:41 local/tmm1 info tmm1[5213]: Rule https_www_redirect : 203.12.2.160:50661: GET request to domain.com/
 Jun 15 11:30:41 local/tmm1 info tmm1[5213]: Rule https_www_redirect : 203.12.2.160:50661: Redirecting to https://domain.com/
 Jun 15 11:30:43 local/tmm info tmm[5212]: Rule https_www_redirect : 103.63.211.158:1146: HEAD request to /
 Jun 15 11:30:43 local/tmm info tmm[5212]: Rule https_www_redirect : 103.63.211.158:1146: Redirecting to https:///
 Jun 15 11:30:47 local/tmm info tmm[5212]: Rule https_www_redirect : 103.63.211.158:23950: GET request to domain.com/
 Jun 15 11:30:47 local/tmm info tmm[5212]: Rule https_www_redirect : 103.63.211.158:23950: Redirecting to https://domain.com/
 Jun 15 11:31:43 local/tmm info tmm[5212]: Rule https_www_redirect : 103.63.211.158:1664: HEAD request to /
 Jun 15 11:31:43 local/tmm info tmm[5212]: Rule https_www_redirect : 103.63.211.158:1664: Redirecting to https:///
 Jun 15 11:32:44 local/tmm info tmm[5212]: Rule https_www_redirect : 103.63.211.158:2210: HEAD request to /
 Jun 15 11:32:44 local/tmm info tmm[5212]: Rule https_www_redirect : 103.63.211.158:2210: Redirecting to https:///
 Jun 15 11:33:45 local/tmm info tmm[5212]: Rule https_www_redirect : 103.63.211.158:2880: HEAD request to /
 Jun 15 11:33:45 local/tmm info tmm[5212]: Rule https_www_redirect : 103.63.211.158:2880: Redirecting to https:///
 Jun 15 11:34:45 local/tmm1 info tmm1[5213]: Rule https_www_redirect : 103.63.211.158:3407: HEAD request to /
 Jun 15 11:34:45 local/tmm1 info tmm1[5213]: Rule https_www_redirect : 103.63.211.158:3407: Redirecting to https:///
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