Forum Discussion
theizer_91634
Nimbostratus
Apr 01, 2009multiple redirects - newb needs help
ok...trying my hand at iRules
we own domain.com and domain.net
I need my rule to redirect the following:
1. http://domain.com to https://www.domain.com
2. http://domain.net to https://www.domain.com
3. http://[anything].domain.net to https://www.domain.com
4. http://[anything not "www"].domain.com to https://www.domain.com
5. http://www.domain.com/[path] to https://www.domain.com/[path]
Here is what I have so far. I know I am missing a lot and would really appreciate any help you guys can give.
when HTTP_REQUEST {
if {[HTTP::uri] equals {http://domain.com}} {HTTP::redirect https://www.domain.com
}
elseif {[HTTP::request] contains "domain.net"} {HTTP::redirect https://www.domain.com
}
else
{HTTP::redirect https://[HTTP::host][HTTP::uri]}
}
3 Replies
- dennypayne
Employee
Hi,
One problem is the domain.com, domain.net, etc is not part of [HTTP::uri], it's the [HTTP::host]. URI is the rest of the URL after the host, so your first check would need to be against [HTTP::host] without the http://.
That said, I think you could just do:when HTTP_REQUEST { if { not [HTTP::host] equals "www.domain.com" }{ HTTP::redirect "https://www.domain.com" } else { HTTP::redirect "https://www.domain.com/[HTTP::uri]" } }
Denny - theizer_91634
Nimbostratus
Thanks for the reply!
I tried your suggestion, but I think I may have something wrong with syntax as nothing redirects when I apply this rule. The iRule editor says it is valid though.
when HTTP_REQUEST {
if { not [HTTP::host] equals "www.domain.com" }{
HTTP::redirect https://www.domain.com
} else {
HTTP::redirect https://[HTTP:host][HTTP::uri]
}
} - hoolio
Cirrostratus
Try wrapping the condition you're NOT'ing in parentheses:when HTTP_REQUEST { if { not ([string tolower [HTTP::host]] equals "www.domain.com") }{ HTTP::redirect https://www.domain.com } else { HTTP::redirect "https://[HTTP:host][HTTP::uri]" } }
Or this way:when HTTP_REQUEST { if { [string tolower [HTTP::host]] equals "www.domain.com" }{ HTTP::redirect https://[HTTP:host][HTTP::uri] } else { HTTP::redirect "https://www.domain.com" } }
Also, because of a bug in the iRule parser in pre-v10 versions, make sure to always wrap // in double quotes. See SOL7988 for details:
SOL7988: The iRule parser may fail to correctly parse and load an iRule from the command line (Click here)
Aaron
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