Forum Discussion
Chris_DiPietro_
Mar 08, 2012Nimbostratus
redirecting urls to add www to the host if it does not exists
I have two VSs one responds to port 80 but redirects to https and one responds to HTTPS, but I want to make sure anyone trying to get to sample.com gets redirected to www.sample.com so the SSL cert matches correctly
I wrote the following iRules based on what I have found in other posts and I just want to make sure these are correct
For the port 80 VS
when HTTP_REQUEST {
if
{[string tolower [HTTP::host]]
starts_with "www"} {
HTTP::respond 301 Location ""
} else {
HTTP::respond 301 Location "":" 1][HTTP::uri]"
}
}
for the port 443 VS
when HTTP_REQUEST {
if { not ([string tolower [HTTP::host]]
starts_with "www") } {
HTTP::respond 301 Location "":" 1][HTTP::uri]"
}
}
If this is wrong or there is a cleaner/better way to do this please let me know
Thanks
- hooleylistCirrostratusIf your cert is only valid for www.sample.com, why not redirect all HTTP requests to https://www.sample.com?
when HTTP_REQUEST { HTTP::redirect "https://www.sample.com[HTTP::uri]" }
- Chris_DiPietro_NimbostratusThe only reason I didn't just redirect all requests to the actual domain was a case where a developer had typed in the IP address and was trying to test it directly so that it would preserve the IP when it redirected.
- hooleylistCirrostratusIf you want to avoid redirecting IP addresses with www. prepended, you could use something like this:
when HTTP_REQUEST { Check if Host header starts with a digit (is an IP address) if {[string match {[0-9]*]} [HTTP::host]}{ HTTP::redirect "https://[HTTP::host][HTTP::uri]" } else { HTTP::redirect "https://www.sample.com[HTTP::uri]" } }
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