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
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
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 }]
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. - 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" } }
- 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" } }
Hello,
You can add the following line below your HTTP::redirect command
log local0. "[HTTP::host] - client ip : [IP::client_addr]"Then you can see logs at /var/log/ltm or via GUI System -> Logs -> Local Traffic
- ShakN_167332
Nimbostratus
Hi yaan, How to add logging to this irule to check the incoming urla nd uri and redirected url and uri.. f { [HTTP::host] equals "asites.abc.com" and [HTTP::uri] starts_with "/bseu/finance" } { HTTP::redirect "http://asites.abc.com/sites/finance" }
- Yann_Desmarest_
Nacreous
Hello,
You can add the following line below your HTTP::redirect command
log local0. "[HTTP::host] - client ip : [IP::client_addr]"Then you can see logs at /var/log/ltm or via GUI System -> Logs -> Local Traffic
- ShakN_167332
Nimbostratus
Hi yaan, How to add logging to this irule to check the incoming urla nd uri and redirected url and uri.. f { [HTTP::host] equals "asites.abc.com" and [HTTP::uri] starts_with "/bseu/finance" } { HTTP::redirect "http://asites.abc.com/sites/finance" }
when HTTP_REQUEST { if {[HTTP::host] equals "awww.abc.com"} { HTTP::redirect "http://aportal.abc.com[HTTP::uri]" log local0. "[HTTP::host] - client ip : [IP::client_addr] - Redirect to http://aportal.abc.com[HTTP::uri]" } if { [HTTP::host] equals "asites.abc.com" and [HTTP::uri] starts_with "/bseu/finance" } { log local0. "[HTTP::host] - client ip : [IP::client_addr] - Before URI : [HTTP::uri], Redirect to http://asites.abc.com/sites/finance" HTTP::redirect "http://asites.abc.com/sites/finance" } }- ShakN_167332
Nimbostratus
Hi thank you so much it worked. - ShakN_167332
Nimbostratus
i code one more code log local0. "Incoming URI = [HTTP::uri]" if { [string tolower [HTTP::uri]] starts_with "/bseu" } { set uri [string map -nocase {"/bseu" "/sites"} [HTTP::uri]] log local0. "New URI = $uri" HTTP::uri $uri } } now client requirement changed, above code is working where it is redirecting all the sites to required uri but client wants 2 sites which is /bseu and /bseu/it should not redirect according to the code. please suggest how this can be achived.
- Yann_Desmarest_
Nacreous
when HTTP_REQUEST { if {[HTTP::host] equals "awww.abc.com"} { HTTP::redirect "http://aportal.abc.com[HTTP::uri]" log local0. "[HTTP::host] - client ip : [IP::client_addr] - Redirect to http://aportal.abc.com[HTTP::uri]" } if { [HTTP::host] equals "asites.abc.com" and [HTTP::uri] starts_with "/bseu/finance" } { log local0. "[HTTP::host] - client ip : [IP::client_addr] - Before URI : [HTTP::uri], Redirect to http://asites.abc.com/sites/finance" HTTP::redirect "http://asites.abc.com/sites/finance" } }- ShakN_167332
Nimbostratus
Hi thank you so much it worked. - ShakN_167332
Nimbostratus
i code one more code log local0. "Incoming URI = [HTTP::uri]" if { [string tolower [HTTP::uri]] starts_with "/bseu" } { set uri [string map -nocase {"/bseu" "/sites"} [HTTP::uri]] log local0. "New URI = $uri" HTTP::uri $uri } } now client requirement changed, above code is working where it is redirecting all the sites to required uri but client wants 2 sites which is /bseu and /bseu/it should not redirect according to the code. please suggest how this can be achived.
Hello,
You can add an if condition
log local0. "Incoming URI = [HTTP::uri]" if { [string tolower [HTTP::uri]] starts_with "/bseu" } { if { [string tolower [HTTP::uri]] equals "/bseu" or [string tolower [HTTP::uri]] starts_with "/bseu/it" } { set uri [HTTP::uri] } else { set uri [string map -nocase {"/bseu" "/sites"} [HTTP::uri]] } log local0. "New URI = $uri" HTTP::uri $uri } }- ShakN_167332
Nimbostratus
when i use this code my redirection is working as expected but stopps working after first time testing. if there any issue with the code. can you please help me.
- Yann_Desmarest_
Nacreous
Hello,
You can add an if condition
log local0. "Incoming URI = [HTTP::uri]" if { [string tolower [HTTP::uri]] starts_with "/bseu" } { if { [string tolower [HTTP::uri]] equals "/bseu" or [string tolower [HTTP::uri]] starts_with "/bseu/it" } { set uri [HTTP::uri] } else { set uri [string map -nocase {"/bseu" "/sites"} [HTTP::uri]] } log local0. "New URI = $uri" HTTP::uri $uri } }- ShakN_167332
Nimbostratus
when i use this code my redirection is working as expected but stopps working after first time testing. if there any issue with the code. can you please help me.
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