Forum Discussion
Ryan_McDonald_8
Nimbostratus
Aug 07, 2009Redirect to www
If a customer types in domainname.com I want them to be redirected to www.domainname.com.
I used the irule generator and came up with this
when HTTP_REQUEST {
if {...
Aug 07, 2009
A couple of points.
First, The URL is in the form of protocol://[HTTP::host][HTTP::uri]. In your case, if you are searching for a domain, then you can just reference the [HTTP::host] variable.
For hostname modifications there are two ways to go about it. First, you can issue a HTTP redirect to the browser with the HTTP::redirect command. This will send a response back to the browser with the new address and the browsers address bar will be updated with the new address. Second, you could transparently change the host name my modifying the Host header with the HTTP::header command. This will not send an update to the browser but only change what the backend server "thinks" was typed in the browsers address bar.
It seems that you want to issue a full redirect to the client so this example should do the trick.
when HTTP_REQUEST {
if { [string tolower [HTTP::host]] eq "scadalynx.com" } {
HTTP::redirect "http://www.[HTTP::host][HTTP::uri]"
}
}
This will keep the existing hostname but prepend a www to it. So a request from http://foo.com/index.html will be redirected to http://www.foo.com/index.html.
If you need this for your SSL virtual as well, it will be easiest to have two iRules with different redirect statements. If you really want one, you can get the protocol from the TCP::local_port. If that equals 443 then you can use "https" instead of "http" in the redirect. Something like this should work.
when HTTP_REQUEST {
if { [string tolower [HTTP::host]] eq "scadalynx.com" } {
if { [TCP::local_port] == 443 } {
HTTP::redirect "https://www.[HTTP::host][HTTP::uri]"
} else {
HTTP::redirect "http://www.[HTTP::host][HTTP::uri]"
}
}
}
Hope this helps...
-Joe
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)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