Forum Discussion
I want to Redirect multiple Sites in a single Irule. please help me with the irule command.
I have mulitple sites where i want my LAN users should not to redirected to https when they use url with http. but when any internet users use http://abc.com than it should redirect to https://abc.com
Can i use the below irule for this.
when HTTP_REQUEST { if {[HTTP::host] equals "abc.com"} { HTTP::redirect "https://abc.com[HTTP::uri]" } }
Now i want to do for multiple sites in same irule can i use another if statement?
49 Replies
This irule will work. You may change it to fit for your need of multiple sites :
when HTTP_REQUEST { switch -glob [HTTP::host] { "hostname1" { HTTP::redirect "https://hostname1[HTTP::uri] } "hostname1" { HTTP::redirect "https://hostname1[HTTP::uri] } "hostname2" { HTTP::redirect "https://hostname2[HTTP::uri] } default { } } }If this is just for redirecting http to https, you can just use this irule :
when HTTP_REQUEST { HTTP::redirect "https://[HTTP::host][HTTP::uri]" }- nitass
Employee
I have mulitple sites where i want my LAN users should not to redirected to https when they use url with http. but when any internet users use http://abc.com than it should redirect to https://abc.com
you may use IP::addr to check whether it is lan user traffic and then redirect if not.
e.g.
config [root@ve11a:Standby:In Sync] config tmsh list ltm virtual bar ltm virtual bar { destination 172.28.24.10:80 ip-protocol tcp mask 255.255.255.255 pool foo profiles { http { } tcp { } } rules { qux } source 0.0.0.0/0 source-address-translation { type automap } vs-index 65 } [root@ve11a:Standby:In Sync] config tmsh list ltm rule qux ltm rule qux { when HTTP_REQUEST { if { !([IP::addr [IP::client_addr] equals 172.28.24.0/24]) } { HTTP::redirect "https://[HTTP::host][HTTP::uri]" } } } test1 (client ip is 172.28.24.11) [root@ve11a:Standby:In Sync] config curl -I http://172.28.24.10/ HTTP/1.1 200 OK Date: Wed, 20 Aug 2014 07:08:10 GMT Server: Apache/2.2.3 (CentOS) Last-Modified: Sun, 09 Feb 2014 08:39:51 GMT ETag: "41879c-59-2a9c23c0" Accept-Ranges: bytes Content-Length: 89 Content-Type: text/html; charset=UTF-8 test2 (client ip is 172.28.77.29) curl -I http://172.28.24.10/ HTTP/1.0 302 Found Location: https://172.28.24.10/ Server: BigIP Connection: Keep-Alive Content-Length: 0 - ShakN_167332
Nimbostratus
Thank you that was really helpful. I need more help.
in our setup we have sites which are accessed by internal users as well as internet users via http and https. so we have created 4 VS (2 internal VS, one for http and one for https for internal users) and (2 for internet users 1 for http and one for https). our requirement is any lan user access sites via http than it should be http if lan users type https than it should redirect to http. similarly if any internet users access sites via http than it should redirect to https. this is done by 1 irule one for http to https redirection.
when RULE_INIT { set static::bsint "internet.abc.com" set static::sitesint "sites.internet.abc.com"
} when HTTP_REQUEST { if {[class match [IP::client_addr] equals InternalHosts]} {
HTTP::redirect http://[HTTP::host][HTTP::uri] } else { switch -glob -- [string tolower [HTTP::host]] { "sites.abc.com" { HTTP::header replace Host $static::sitesint STREAM::disable } "abc.com" { HTTP::header replace Host $static::bsint STREAM::disable }}} } when HTTP_RESPONSE { if {[HTTP::is_redirect] && [string tolower [HTTP::header "Location"]] contains $static::sitesint}{ HTTP::header replace Location [string map "$static::sitesint $static::sitesext" [HTTP::header Location]] } elseif {[HTTP::is_redirect] && [string tolower [HTTP::header "Location"]] contains $static::bsint}{ HTTP::header replace Location [string map "$static::bsint $static::bsext" [HTTP::header Location]]
}
in addition to it i need to redirect below redirection in same irule please guide me how to do this.
if any one access from internet to the below url than it should redirect to https://asites.abc.com/xyz/finance
redirection to below url
http://asites.internet.abc.com/sites/finance
and many more sites like this.
please some one help me in this.thank you in advance.
Hello,
Maybe you can test this peace of code :
if {[PROFILE::exists clientssl] == 0}{ if { [HTTP::host] contains "asites.abc.com" and [string match "/*/finance*" [HTTP::uri]] } { set new_host [string map -nocase [list "abc.com" "internet.abc.com"] [HTTP::host]] HTTP::redirect https://$new_host[HTTP::uri] } }Not Sure that string match will works that way need to test.
- ShakN_167332
Nimbostratus
thank you let me try this and i will get back to you. - ShakN_167332
Nimbostratus
Hi Yann Desmarest, I have tired the suggestion for multiple redirection but it dint worked. can i use the below code. when HTTP_REQUEST { if { [HTTP::host] equals "asites.abc.com" and [HTTP::uri] equals "/xyz/finance" } { HTTP::redirect "http://asites.internet.abc.com/sites/finance" } } if so how to modify this for similar 22 sites redirection.
- Yann_Desmarest_
Nacreous
Hello,
Maybe you can test this peace of code :
if {[PROFILE::exists clientssl] == 0}{ if { [HTTP::host] contains "asites.abc.com" and [string match "/*/finance*" [HTTP::uri]] } { set new_host [string map -nocase [list "abc.com" "internet.abc.com"] [HTTP::host]] HTTP::redirect https://$new_host[HTTP::uri] } }Not Sure that string match will works that way need to test.
- ShakN_167332
Nimbostratus
thank you let me try this and i will get back to you. - ShakN_167332
Nimbostratus
Hi Yann Desmarest, I have tired the suggestion for multiple redirection but it dint worked. can i use the below code. when HTTP_REQUEST { if { [HTTP::host] equals "asites.abc.com" and [HTTP::uri] equals "/xyz/finance" } { HTTP::redirect "http://asites.internet.abc.com/sites/finance" } } if so how to modify this for similar 22 sites redirection.
- Yann_Desmarest_
Nacreous
Hello,
What part of your site will change when you mentionned 22 sites :
hostname, URI, or both ?
Can you provide me with a sample of sites ?
Yann
- ShakN_167332
Nimbostratus
hi yann, yes see below is my requirement. both hostbname and uri. need to do similar config for 22 sites,. https://asites.abc.com/xyz/finance should redirect to below url http://asites.internet.abc.com/sites/finance
Hello,
What part of your site will change when you mentionned 22 sites :
hostname, URI, or both ?
Can you provide me with a sample of sites ?
Yann
- ShakN_167332
Nimbostratus
hi yann, yes see below is my requirement. both hostbname and uri. need to do similar config for 22 sites,. https://asites.abc.com/xyz/finance should redirect to below url http://asites.internet.abc.com/sites/finance
- Yann_Desmarest_
Nacreous
Maybe you can try this
when HTTP_REQUEST { if { [HTTP::host] equals "asites.abc.com" } { switch -glob [HTTP::uri] { "/xyz/finance" - "/xyz/site2" - "/xyz/site3" - "/xyz/site4" - "/xyz/site5" { HTTP::redirect "http://asites.internet.abc.com[string map -nocase [list "/xyz/finance" "/sites/finance" "/xyz/test" "/sites/test"] [HTTP::uri]]" } default {} } }You can also use datagroup to achieve what you want
when HTTP_REQUEST { if { [HTTP::host] equals "asites.abc.com" } { if { class match [HTTP::uri] equals mydatagroup } { HTTP::redirect "http://asites.internet.abc.com[class lookup [HTTP::uri] mydatagroup]" } } }where datagroup of type string contains "/xyz/finance" with value "/sites/finance"
- ShakN_167332
Nimbostratus
Will this irule work for my internet traffic redirection from https to http. when HTTP_REQUEST { if {[class match [IP::client_addr] equals InternalHosts]} { HTTP::redirect http://[HTTP::host][HTTP::uri] } else { switch -glob -- [string tolower [HTTP::host]] { "https://asites.abc.com/bseu/finance" { HTTP::redirect "http://asites.internet.abc.com/sites/finance" } } } - Yann_Desmarest_
Nacreous
It will not work as HTTP::host give you only asites.abc.com as result. You can do the following : switch -glob -- [string tolower "[HTTP::host][HTTP::uri]"] { "asites.abc.com/bseu/finance" ... You can't know you are in https or http because you are in the HTTP_REQUEST event I suggest you to add the following : if {[PROFILE::exists clientssl] == 0}{ set proto "http" } else { set proto "https" } ... switch -glob -- [string tolower "$proto://[HTTP::host][HTTP::uri]"] { "http://asites.abc.com/bseu/finance" ... - ShakN_167332
Nimbostratus
i get the below error when i try to create the data group which you suggested in irule. 01070151:3: Rule [test] error: line 3: [parse error: PARSE syntax 104 {syntax error in expression " class match [HTTP::uri] equals test ": variable references require preceding $}] [{ class match [HTTP::uri] equals test }]
Maybe you can try this
when HTTP_REQUEST { if { [HTTP::host] equals "asites.abc.com" } { switch -glob [HTTP::uri] { "/xyz/finance" - "/xyz/site2" - "/xyz/site3" - "/xyz/site4" - "/xyz/site5" { HTTP::redirect "http://asites.internet.abc.com[string map -nocase [list "/xyz/finance" "/sites/finance" "/xyz/test" "/sites/test"] [HTTP::uri]]" } default {} } }You can also use datagroup to achieve what you want
when HTTP_REQUEST { if { [HTTP::host] equals "asites.abc.com" } { if { class match [HTTP::uri] equals mydatagroup } { HTTP::redirect "http://asites.internet.abc.com[class lookup [HTTP::uri] mydatagroup]" } } }where datagroup of type string contains "/xyz/finance" with value "/sites/finance"
- ShakN_167332
Nimbostratus
Will this irule work for my internet traffic redirection from https to http. when HTTP_REQUEST { if {[class match [IP::client_addr] equals InternalHosts]} { HTTP::redirect http://[HTTP::host][HTTP::uri] } else { switch -glob -- [string tolower [HTTP::host]] { "https://asites.abc.com/bseu/finance" { HTTP::redirect "http://asites.internet.abc.com/sites/finance" } } } - It will not work as HTTP::host give you only asites.abc.com as result. You can do the following : switch -glob -- [string tolower "[HTTP::host][HTTP::uri]"] { "asites.abc.com/bseu/finance" ... You can't know you are in https or http because you are in the HTTP_REQUEST event I suggest you to add the following : if {[PROFILE::exists clientssl] == 0}{ set proto "http" } else { set proto "https" } ... switch -glob -- [string tolower "$proto://[HTTP::host][HTTP::uri]"] { "http://asites.abc.com/bseu/finance" ...
- ShakN_167332
Nimbostratus
i get the below error when i try to create the data group which you suggested in irule. 01070151:3: Rule [test] error: line 3: [parse error: PARSE syntax 104 {syntax error in expression " class match [HTTP::uri] equals test ": variable references require preceding $}] [{ class match [HTTP::uri] equals test }]
- Yann_Desmarest_
Nacreous
Hello,
There is missing bracket :
[class match [HTTP::uri] equals test]
- ShakN_167332
Nimbostratus
it is not working do i ahve to use the full url. http://asites.abc.com/xyz/finance/SitePages/Home.aspx do i have to use full url like after /xyz/finance it is sitesPages/homes.aspx. - Yann_Desmarest_
Nacreous
can you repost the full irule you are trying to test ? - ShakN_167332
Nimbostratus
when HTTP_REQUEST { if {[HTTP::host] equals "awww.abc.com"} { HTTP::redirect "http://aportal.abc.com[HTTP::uri]" } if { [HTTP::host] equals "asites.abc.com" and [HTTP::uri] starts_with "/bseu/finance" } { HTTP::redirect "http://asites.abc.com/sites/finance" } }
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