Forum Discussion
iRule hlep
I need to redirect from http to https , also since some users have bookmark specific port for exmaple port 8050 in their browser to be redirect to https. how do I do that.
does this works?
when HTTP_REQUEST {
if { ([HTTP::uri] eq "/")}{
HTTP::redirect ""
}
}
when HTTP_REQUEST_SEND {
clientside {
if { ( ($example_host eq "example.com") } {
HTTP::header replace Host "example.com:8050"
}
}
}
Thx
14 Replies
- KJ_50941
Nimbostratus
How about this one?
when HTTP_REQUEST {
if {[HTTP::uri] equals {http://example:8050}} {HTTP::uri {https://example.com}
} else {
HTTP::redirect https://[IP::local_addr][HTTP::uri]
}
}
}
}Thx all
- nathe
Cirrocumulus
KJ
I'm not 100% sure of your requirements but this is a simple irule to redirect all http requests to the vs to https and removes any non-default port. Taken from: https://devcentral.f5.com/wiki/iRules.HTTPToHTTPSRedirect_302.ashxwhen HTTP_REQUEST { HTTP::redirect https://[getfield [HTTP::host] ":" 1][HTTP::uri] }
Hope this helps,
N - KJ_50941
Nimbostratus
Thx, our requiremnets is to redirect HTTP to HTTPS and secondly if some user has bookmark port:8050 they also need to redirect to https.
does this will work?
when HTTP_REQUEST {
HTTP::redirect https://[getfield [HTTP::host] ":" 1][HTTP::uri]
}or
when HTTP_REQUEST {
if {[TCP::local_port] != 443}{
HTTP::redirect " [HTTP::host] ":" 1][HTTP::uri]"
}
}
}also what does getfield do?
thx in advanced. - nathe
Cirrocumulus
KJ,
The getfield bit is stripping out the port, if it's a non-default one. This should cover both bases, no port specified and 8050 in any bookmarks.
N - afedden_1985
Cirrus
Not sure if this will apply but I have used this when we know what the URI starts with and we want to redirect them to a different path on the same host, this uses a 301 redirect instead of the normal 302
when HTTP_REQUEST {
if { [string toupper [HTTP::uri]] starts_with "/PATH1/PATH2/" }{
HTTP::respond 301 Location "https://example.com/newpath1/"
}
} - Jnon
Nimbostratus
KJ
If I understand correctly you are just needing to redirect everything that isn't 443 I would just put up two virtuals with the same IP one listening on ANY port and the other listening on 443 then just put in a http to https redirect on the ANY listener. The more specific listener will override the ANY to prevent any looping. - nitass
Employee
e.g.[root@ve10:Active] config b virtual wildcard list virtual wildcard { destination 172.28.19.79:any ip protocol 6 rules myrule profiles { http {} tcp {} } } [root@ve10:Active] config b rule myrule list rule myrule { when HTTP_REQUEST { log local0. "\[HTTP::host\] = [HTTP::host]" log local0. "\[getfield [HTTP::host] : 1\] = [getfield [HTTP::host] : 1]" log local0. "\[HTTP::uri\] = [HTTP::uri]" HTTP::redirect https://[getfield [HTTP::host] : 1][HTTP::uri] } } [root@ve10:Active] config b virtual bar list virtual bar { snat automap pool foo destination 172.28.19.79:443 ip protocol 6 profiles { clientssl { clientside } tcp {} } } on client curl -I http://172.28.19.79:8050/something -H "Host: www.somedomain.com:8050" HTTP/1.0 302 Found Location: https://www.somedomain.com/something Server: BigIP Connection: Keep-Alive Content-Length: 0 on F5 [root@ve10:Active] config tail -f /var/log/ltm Sep 30 20:53:10 local/tmm info tmm[4925]: Rule myrule : [HTTP::host] = www.somedomain.com:8050 Sep 30 20:53:10 local/tmm info tmm[4925]: Rule myrule : [getfield www.somedomain.com:8050 : 1] = www.somedomain.com Sep 30 20:53:10 local/tmm info tmm[4925]: Rule myrule : [HTTP::uri] = /something - KJ_50941
Nimbostratus
Thank you for your help. - KJ_50941
Nimbostratus
I already have VS_80 and VS_443 and applied redirect from 80 to 443, do I need to change 80 to all port or create new VIP with all port, and can this iRule work for redirect on *_VS ? - KJ_50941
Nimbostratus
I already have VS_80 and VS_443 and applied redirect from 80 to 443, do I need to change 80 to all port or create new VIP with all port, and can this iRule work for redirect on *_VS ?
when HTTP_REQUEST {
HTTP::redirect https://[HTTP::host][HTTP::uri]
}
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)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
