Forum Discussion
Inefficient iRule, works but not perfect
We have a working iRule that I'm sure can be improved/. The first section is simple redirects from domain.org URL to equivalent https domain.com URL. We discovered that if users browsed directly to redirected URL (http://team.domain.com for example) they got nothing so we added the second section to address that. This works but with a few issues:
- browse to http://team.domain.com and it redirects to https://team.domain.com/default.aspx/ (trailing slash not desired)
- remove s from https from redirected site, hit enter and you get https://team.domain.com/default.aspx/default.aspx/ (multiple /default.aspx/)
- If you continue removing s from https it will keep creating these /default.aspx/,
It works still but isn't too pretty. Any help would be appreciated by a beginner..
when HTTP_REQUEST {
if { [HTTP::host] equals "team.domain.org" } {
HTTP::redirect "https://team.domain.com[HTTP::uri]"}
if { [HTTP::host] equals "site1.domain.org" } {
HTTP::redirect "https://site1.domain.com[HTTP::uri]"}
if { [HTTP::host] equals "site2.domain.org" } {
HTTP::redirect "https://site2.domain.com[HTTP::uri]"}
if { [HTTP::host] equals "myteam.domain.org" } {
HTTP::redirect "https://myteam.domain.com[HTTP::uri]"}
if { [HTTP::host] equals "team.domain.com" } {
HTTP::redirect "https://team.domain.com/default.aspx[HTTP::uri]"}
if { [HTTP::host] equals "site1.domain.com" } {
HTTP::redirect "https://site1.domain.com/default.aspx[HTTP::uri]"}
if { [HTTP::host] equals "site2.domain.com" } {
HTTP::redirect "https://site2.domain.com/default.aspx[HTTP::uri]"}
if { [HTTP::host] equals "myteam.domain.com" } {
HTTP::redirect "https://myteam.domain.com/default.aspx[HTTP::uri]"}
}
12 Replies
- Stanislas_Piro2
Cumulonimbus
Use the switch statement instead of if and use variable if possible...
when HTTP_REQUEST { switch -glob [string tolower [HTTP::host]] { "team.domain.org" { HTTP::redirect "https://team.domain.com[HTTP::uri]"} "site1.domain.org" { HTTP::redirect "https://site1.domain.com[HTTP::uri]"} "site2.domain.org" { HTTP::redirect "https://site2.domain.com[HTTP::uri]"} "myteam.domain.org" { HTTP::redirect "https://myteam.domain.com[HTTP::uri]"} "site1.domain.com" - "site2.domain.com" - "team.domain.com" - "myteam.domain.com" { https://[HTTP::host]/default.aspx[HTTP::uri]"} } }
- nathe
Cirrocumulus
is this neater and more efficient? let me know if it doesn't meet all your requirements. Away from the lab at the moment to test.
when HTTP_REQUEST { switch -glob [string tolower [HTTP::host]] { "team.domain.org" { HTTP::respond 301 Location "https://team.domain.com[HTTP::uri]" } "site1.domain.org" { HTTP::respond 301 Location "https://site1.domain.com[HTTP::uri]" } "site2.domain.org" { HTTP::respond 301 Location "https://site2.domain.com[HTTP::uri]" } "myteam.domain.org" { HTTP::respond 301 Location "https://myteam.domain.com[HTTP::uri]" } "*.domain.com" { HTTP::respond 301 Location "https://[HTTP::host]/default.aspx" } } }
Obviously a 301 is permanent redirect, i've assumed this.
N
- Stanislas_Piro2
Cumulonimbus
I did not see the loop of default.aspx was cause by : /default.aspx[HTTP::uri] the redirect must be :HTTP::redirect "https://myteam.domain.com/default.aspx - kraigk_52257
Nimbostratus
Nathan,
Thanks - I'm going to test this today and will get back to you. Much cleaner than my mess.
- nathe
Cirrocumulus
No probs. Be good to hear your feedback
- Frank_30530
Altocumulus
What will be the effect if the Host: request header matches neither of the strings? I.e., always write explicit code for the 'default' case...
Just my $0.02 worth :-)
-Frank
- kraigk_52257
Nimbostratus
Nathan, I did finally get to test and it works well. We added short names as well as they were hitting the vs and failing. Not sure if there is a better way to incorporate them into this. Thanks for your suggestions.
when HTTP_REQUEST { switch -glob [string tolower [HTTP::host]] { "team" { HTTP::respond 301 Location "https://team.domain.com[HTTP::uri]" } "site1" { HTTP::respond 301 Location "https://site1.domain.com[HTTP::uri]" } "site2" { HTTP::respond 301 Location "https://site2.domain.com[HTTP::uri]" } "myteam" { HTTP::respond 301 Location "https://myteam.domain.com[HTTP::uri]" } "team.domain.org" { HTTP::respond 301 Location "https://team.domain.com[HTTP::uri]" } "site1.domain.org" { HTTP::respond 301 Location "https://site1.domain.com[HTTP::uri]" } "site2.domain.org" { HTTP::respond 301 Location "https://site2.domain.com[HTTP::uri]" } "myteam.domain.org" { HTTP::respond 301 Location "https://myteam.domain.com[HTTP::uri]" } "*.domain.com" { HTTP::respond 301 Location "https://[HTTP::host]/default.aspx" } } }
- nathe
Cirrocumulus
Could you use a wildcard?
when HTTP_REQUEST { switch -glob [string tolower [HTTP::host]] { "team*" { HTTP::respond 301 Location "https://team.domain.com[HTTP::uri]" } "site1*" { HTTP::respond 301 Location "https://site1.domain.com[HTTP::uri]" } "site2*" { HTTP::respond 301 Location "https://site2.domain.com[HTTP::uri]" } "myteam*" { HTTP::respond 301 Location "https://myteam.domain.com[HTTP::uri]" } "*.domain.com" { HTTP::respond 301 Location "https://[HTTP::host]/default.aspx" } } }
- kraigk_52257
Nimbostratus
I can certainly try. Will give it a go.
- nathe
Cirrocumulus
Good luck. Mark up if it works. Ta
- kraigk_52257
Nimbostratus
Nathan, I tested and implemented exactly what you suggested and it works perfectly. I really appreciate your help.
when HTTP_REQUEST { switch -glob [string tolower [HTTP::host]] { "team*" { HTTP::respond 301 Location "https://team.domain.com[HTTP::uri]" } "site1*" { HTTP::respond 301 Location "https://site1.domain.com[HTTP::uri]" } "site2*" { HTTP::respond 301 Location "https://site2.domain.com[HTTP::uri]" } "myteam*" { HTTP::respond 301 Location "https://myteam.domain.com[HTTP::uri]" } "*.domain.com" { HTTP::respond 301 Location "https://[HTTP::host]/default.aspx" } } }
- nathe
Cirrocumulus
Great to hear!
Recent Discussions
Related Content
* 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