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
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,
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,
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" }
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
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.
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
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.
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.
- ShakN_167332
Nimbostratus
this is my irule and applied on vs 443. my /bseu to /sites redirection works first time and next time onwords redirection is not happening.
when RULE_INIT { set static::asitesext "asites.abc.com" set static::asitesint "asites.internet.abc.com" set static::aportalext "aportal.abc.com" set static::aportalint "aportal.internet.abc.com" set static::amyext "amy.abc.com" set static::amyint "amy.internet.abc.com" set static::aextranetext "aextranet.abc.com" set static::aextranetint "aextranet.internet.abc.com" set static::awww "awww.abc.com" } when HTTP_REQUEST { if {[class match [IP::client_addr] equals InternalHosts]} {
HTTP::redirect http://[HTTP::host][HTTP::uri] } else { 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 }switch -glob -- [string tolower [HTTP::host]] { "asites.abc.com" { HTTP::header replace Host $static::asitesint STREAM::disable } "awww.abc.com" { HTTP::redirect https://aportal.abc.com STREAM::disable } "aportal.abc.com" { HTTP::header replace Host $static::aportalint STREAM::disable } "amy.abc.com" { HTTP::header replace Host $static::amyint STREAM::disable } "aextranet.abc.com" { HTTP::header replace Host $static::aextranetint STREAM::disable } }} } when HTTP_RESPONSE { if {[HTTP::is_redirect] && [string tolower [HTTP::header "Location"]] contains $static::asitesint}{ HTTP::header replace Location [string map "$static::asitesint $static::asitesext" [HTTP::header Location]] STREAM::enable } elseif {[HTTP::is_redirect] && [string tolower [HTTP::header "Location"]] contains $static::aportalint}{ HTTP::header replace Location [string map "$static::aportalint $static::aportalext" [HTTP::header Location]] STREAM::enable } elseif {[HTTP::is_redirect] && [string tolower [HTTP::header "Location"]] contains $static::amyint}{ HTTP::header replace Location [string map "$static::amyint $static::amyext" [HTTP::header Location]] STREAM::enable } elseif {[HTTP::is_redirect] && [string tolower [HTTP::header "Location"]] contains $static::aextranetint}{ HTTP::header replace Location [string map "$static::aextranetint $static::aextranetext" [HTTP::header Location]] STREAM::enable } }
- StephanManthey
Nacreous
Could you please provide the log output by running the following command while testing: tail -f /var/log/ltm - ShakN_167332
Nimbostratus
please find the below output of the log tail -f/var/log/ltm [admin@xyz:Active] ~ tail -f /var/log/ltm | grep test Sep 2 10:37:39 local/tmm info tmm[4924]: Rule test : Incoming URI = /bseu/finance Sep 2 10:37:39 local/tmm info tmm[4924]: Rule test : New URI = /bseu/finance Sep 2 10:38:13 local/tmm info tmm[4924]: Rule test : Incoming URI = /favicon.ico Sep 2 10:38:23 local/tmm info tmm[4924]: Rule test : Incoming URI = /bseu/hr Sep 2 10:38:23 local/tmm info tmm[4924]: Rule test : New URI = /bseu/hr Sep 2 10:38:31 local/tmm info tmm[4924]: Rule test : Incoming URI = /bseu/it Sep 2 10:38:31 local/tmm info tmm[4924]: Rule test : New URI = /bseu/it Sep 2 10:39:08 local/tmm info tmm[4924]: Rule test : Incoming URI = /bseu/bd Sep 2 10:39:08 local/tmm info tmm[4924]: Rule test : New URI = /bseu/bd Sep 2 10:39:21 local/tmm info tmm[4924]: Rule test : Incoming URI = /bseu/rd Sep 2 10:39:21 local/tmm info tmm[4924]: Rule test : New URI = /bseu/rd Sep 2 10:39:54 local/tmm info tmm[4924]: Rule test : Incoming URI = /bseu/procurement Sep 2 10:39:54 local/tmm info tmm[4924]: Rule test : New URI = /bseu/procurement Sep 2 10:40:03 local/tmm info tmm[4924]: Rule test : Incoming URI = /bseu/markets Sep 2 10:40:03 local/tmm info tmm[4924]: Rule test : New URI = /bseu/markets - ShakN_167332
Nimbostratus
Can any one give me alternative code for my requirement.
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