simple redirection not working as expected - please help

I need requests to http://website.com to be redirected to http://www.website.com (which also needs to be redirected to https)

Here is the irule I created to do this:

when HTTP_REQUEST {

if {[HTTP::uri] equals {website.com}} {

HTTP::uri {www.website.com}

} else {

HTTP::redirect https://[HTTP::host][HTTP::uri]

}

}

When I applied this and opened http://website.com it just redirected to https://website.com. I used the iRule Redirection generator to create the first part of the if/else but it doesn’t appear to be triggering off http://website.com like I thought it would.

Please help a newbie out.

Thanks!

There are two main parts to a common url like “http://website.com.”

1. [HTTP::host] is “website.com”

2. [HTTP::uri] is typically “/” for a home page, it’s basically the part after the host and before queries, paths, etc.

In your case, you’re looking for a URI to equal “website.com” which will never happen. Also, by using HTTP::uri on the line after, you’re essentially rewriting the URI on the way to your pool member. The reason you were redirected to https://website.com is because you met your else statement.

Try this:

when HTTP_REQUEST {
if { [string tolower [HTTP::host]] eq "website.com"} {
HTTP::redirect "https://[HTTP::host][HTTP::uri]" } }

Instead of “eq”, you could also use “starts_with” as some clients/browsers will send you hosts like “website.com.”

The HTTP::uri function doesn’t actually contain the hostname, just the path. You’re looking for:

when HTTP_REQUEST {

if {[string tolower [HTTP::host]] equals “website.com”} {

HTTP::redirect https://www.website.com[HTTP::uri]

}

}

Do you also need http://www.website.com to redirect to https://www.website.com as well? If so apply this iRule to both the port 80 _and_ 443 VIPs:

when HTTP_REQUEST {

if {[string tolower [HTTP::host]] equals “website.com”} {

HTTP::redirect https://www.website.com[HTTP::uri]

} elseif { ([TCP::local_port] == 80) && ([string tolower HTTP::host]] equals “www.website.com”) } {

HTTP::redirect https://[HTTP::host][HTTP::uri]

}

}

F5 support hooked me up and fixed my irule. Thanks guys!

when HTTP_REQUEST { if {[HTTP::host] equals “website.com”} { HTTP::redirect “https://www.website.com[HTTP::uri]” } else { HTTP::redirect “https://[HTTP::host][HTTP::uri]” } }

That iRule should have the same effect regardless of the requested host. So you should be able to simplify the rule a bit and always redirect to https://[HTTP::host][HTTP::uri] without checking the requested host.

when HTTP_REQUEST {
   HTTP::redirect "https://[HTTP::host][HTTP::uri]"
}

Aaron

Theizer…do you want to redirect all traffic to https even if it isn’t website.com? Or did you just want to redirect website.com to https://www.website.com?

Sorry, Chris. I think I misread the original intent. I’m guessing the original poster is trying to handle example.com and www.example.com resolving to the same IP with a single cert valid only for www.example.com. So maybe it would be safer to redirect all requests to https://www.example.com[HTTP::uri] rather than using [HTTP::host] to insert the originally requested host in the redirect?

Aaron