Forum Discussion
Glenn_Ruffus_10
Nimbostratus
Jan 07, 2008Force www in redirect if missing
I asked for and received help a couple of weeks ago regarding an iRule for forcing traffic on an *HTTPS* virtual server to include "www." at the beginning of the URL so that the requested URL would match the URL on the digital certificate. Here is the iRule that was suggested:
when HTTP_REQUEST {
if { not ( [HTTP::host] starts_with "www." ) } {
HTTP::redirect "https://www.[HTTP::host][HTTP::uri]"
}
}
We found that this could not work on an HTTPS virtual server because I needed the redirect to occur before the traffic was unencrypted, which wasn't possible. The SSL negotiation includes comparing the requested URL with the URL on the certificate. The security warning would occur as a result. This would happen before the redirect could be envoked. So, I gave up on trying to do this on the HTTPS virtual server.
Now, I need to make it happen on the HTTP virtual server.
Our existing "redirect to HTTPS" iRule looks like this:
when HTTP_REQUEST {
HTTP::redirect https://[getfield [HTTP::host] ":" 1][HTTP::uri]
}
I would like to combing the logic of the two iRules so that when I attach the iRule to the HTTP virtual server, it will look at the requested URL to see if it has the www. in the URL. If it does, then redirect to HTTPS with the existing URL/URI. If it does not have the www. at the beginning of the URL, then add it to the front of the URL and send to the adjusted URL/URI.
The first iRule above comes very close. It just doesn't have the logic on what to do with the request if the www. *is* there already.
It might be something like: if the requested URL starts with www., then redirect the existing URL/URI to HTTPS, but if the requested URL does not start with www., then add the www. and redirect to HTTPS.
Thanks for the help!
Glenn
- hoolio
Cirrostratus
Like this?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]" } }
when HTTP_REQUEST { if {[HTTP::host] starts_with "www."} { HTTP::redirect "https://[getfield [HTTP::host] : 1][HTTP::uri]" } else { HTTP::redirect "https://www.[getfield [HTTP::host] : 1][HTTP::uri]" } }
- Glenn_Ruffus_10
Nimbostratus
Thanks for the help, Aaron! We've been testing with the iRule that you suggested and it seems to be working very well. I really appreciate your assistance! - hoolio
Cirrostratus
Good to hear. - Yashodeep_99980
Nimbostratus
were you able to attach same iRule to HTTPS virtual server? - Nicolas_Menant
Employee
Hi,
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