Forum Discussion
iRule Newbie learning - need validation
http://www.mydomain.com -> https://www.mydomain.com
http://mydomain.com -> https://www.mydomain.com
The Rule:
when HTTP_REQUEST {
if { not ([string tolower [HTTP::host]] starts_with "www.")}{
HTTP::redirect "https://www.[HTTP::host][HTTP::uri]"
}
else{
HTTP::redirect https://[getfield [HTTP::host] ":" 1][HTTP::uri]
}
Thanks for all your help on this in advanced. I don't have much of a dev area to play with yet so I am trying to be careful before deploying any of these.
Adam Borders
- hooleylistCirrostratusHi Adam,
when HTTP_REQUEST { switch -glob [string tolower [HTTP::host]] { "www.*" { Host started with "www.". Allow request without redirecting it? } "*[a-z]*" { Host is not null and not an IP address HTTP::redirect https://[getfield [HTTP::host] ":" 1][HTTP::uri] } default { Host is null or an IP address HTTP::redirect "https://default.example.com[HTTP::uri]" } } }
- Adam_Borders_10NimbostratusIs there a way I can make this more generic? I would love to be able to set this as a rule I can use on all of my SSL VS without modifying it for each one.
Thanks,
Adam Borders
- hooleylistCirrostratusWhat would you like to redirect requests to if they are to an IP address? Maybe to that same IP, but via HTTPS?
when HTTP_REQUEST { switch -glob [string tolower [HTTP::host]] { "www.*" { Host started with "www.". Allow request without redirecting it? } "*[a-z]*" { Host is not null and not an IP address HTTP::redirect https://[getfield [HTTP::host] ":" 1][HTTP::uri] } "" { Host is null HTTP::redirect "https://default.example.com[HTTP::uri]" } default { Host is an IP address HTTP::redirect "https://[HTTP::host][HTTP::uri]" } } }
- bmoran_10756Nimbostratus
If you would like to cut down on the number of comparisons:
when HTTP_REQUEST { switch -glob [string tolower [HTTP::host]] { "*[a-z]*" { Host is hostname, redirect. HTTP::redirect https://www.[string trimleft [HTTP::host] "www."][HTTP::uri] } "" { Host is null HTTP::redirect "https://default.example.com[HTTP::uri]" } default { Host is an IP address HTTP::redirect "https://[HTTP::host][HTTP::uri]" } }
} - Adam_Borders_10NimbostratusHoolio-
I'm not too worried about people connecting by IP at this time. I will keep it in mind though. Also all of the public addresses for our sites are translated on a firewall before hitting the F5, so I'm not sure the IP redirect would work.
I also have a series of sites under one SAN cert so I would not want to redirect to just one host.
@bmoran- can you explain how this line works? Are you taking every host request and if it has the www. on it, strip it off and add the https://www.?
HTTP::redirect https://www.[string trimleft [HTTP::host] "www."][HTTP::uri]
Thanks again,
Adam
- hooleylistCirrostratus@bmoran- can you explain how this line works? Are you taking every host request and if it has the www. on it, strip it off and add the https://www.?
- hooleylistCirrostratusActually, I was wrong on this. string trim will remove any instances of individual characters--not just the literal string of "www.". So if your host starts with a w that isn't www., you will get an unintended result:
set host [string tolower [HTTP::host]] if {[scan $host {www.%s} host_no_www] == 1 }{ set host $host_no_www } log local0. "\$host: $host"
Recent Discussions
Related Content
* 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