Forum Discussion
iRule Redirection
Hi,
I am new to iRule section.
I have an iRule for http to https redirect and applied to a virtual server, So any traffic coming through my virtual server will be redirected to https.
when HTTP_REQUEST { HTTP::respond 301 "Location" https://[getfield [HTTP::host] ":" 1][HTTP::uri]
}
Now, I want to exclude one specific domain( from http to https redirect.
Say,
http://Xyz.com & http://abc.com should be redirected to https://Xyz.com & https://abc.com
but if the traffic is http://oldsite.com ,it should not be redirected to https.
In Addition, I want the below one : http://oldsite.com should be redirected to http://newsite.com.
When HTTP_REQUEST { if { ([string tolower [HTTP::host]] equals "oldsite.com" and [HTTP::uri] equals "/") or ([string tolower [HTTP::host]] equals "www.oldsite.com" and [HTTP::uri] equals "/")} { HTTP::respond 301 Location "http://newsite.com" }
I want to combine these things and keep it in one iRule. Please help me.
Thanks in Advance.
Thanks, Sivaraj.
16 Replies
- Hannes_Rapp
Nimbostratus
when HTTP_REQUEST { if { [string tolower [HTTP::host]] ends_with "oldsite.com" } { Conditional redirect to http://www.newsite.com (ORIGINAL request URI is retained) HTTP::respond 301 Location "http://www.newsite.com[HTTP::uri]" Connection Close } else { Generic HTTP to HTTPS redirect (ORIGINAL request Host and URI are retained) HTTP::respond 301 Location "https://[HTTP::host][HTTP::uri]" Connection Close } }- Sivaraj_6072_25
Nimbostratus
Thanks Hannes, Just one thing, If the request is with subdirectories "http://www.oldsite.com /xyz/abc" , it should not be redirected to http://www.newsite.com[HTTP::uri]. Instead it should forward the traffic to Pool as a http request. Thanks, Sivaraj. - Hannes_Rapp
Nimbostratus
Moment, i'll scribble it down...
- Hannes_Rapp_162
Nacreous
when HTTP_REQUEST { if { [string tolower [HTTP::host]] ends_with "oldsite.com" } { Conditional redirect to http://www.newsite.com (ORIGINAL request URI is retained) HTTP::respond 301 Location "http://www.newsite.com[HTTP::uri]" Connection Close } else { Generic HTTP to HTTPS redirect (ORIGINAL request Host and URI are retained) HTTP::respond 301 Location "https://[HTTP::host][HTTP::uri]" Connection Close } }- Sivaraj_6072_25
Nimbostratus
Thanks Hannes, Just one thing, If the request is with subdirectories "http://www.oldsite.com /xyz/abc" , it should not be redirected to http://www.newsite.com[HTTP::uri]. Instead it should forward the traffic to Pool as a http request. Thanks, Sivaraj. - Hannes_Rapp_162
Nacreous
Moment, i'll scribble it down...
- Hannes_Rapp_162
Nacreous
when HTTP_REQUEST { if { ([string tolower [HTTP::host]] ends_with "oldsite.com") && ([HTTP::path] eq "/") } { Intercept with a redirect to http://www.newsite.com HTTP::respond 301 Location "http://www.newsite.com/" Connection Close } elseif { ([string tolower [HTTP::host]] ends_with "oldsite.com") && ([HTTP::path] ne "/") } { Forward traffic to HTTP pool pool NameOfMyHTTPpool80 } else { Generic HTTP to HTTPS redirect (ORIGINAL request Host and URI are retained) HTTP::respond 301 Location "https://[HTTP::host][HTTP::uri]" Connection Close } }- Sivaraj_6072_25
Nimbostratus
Hannes, This is perfect. but for some reason the second condition is not working. It is not sending traffic to the Pool. So I get connection reset error when I browse http://newsite.com/xyz. any idea? Thanks, Siva. - Sivaraj_6072_25
Nimbostratus
Small correction, i tried http://oldsite.com/xyz. And sites doesn't load at all. Finally it throws connection reset error. I verified the pool name and everything looks good. Thanks, Sivaraj. - Hannes_Rapp_162
Nacreous
Perhaps a conflict with another iRule? Do you see anything in /var/log/ltm ? Also, please provide output for your VS config: 'tmsh list ltm virtual YourVSName'
- Hannes_Rapp
Nimbostratus
when HTTP_REQUEST { if { ([string tolower [HTTP::host]] ends_with "oldsite.com") && ([HTTP::path] eq "/") } { Intercept with a redirect to http://www.newsite.com HTTP::respond 301 Location "http://www.newsite.com/" Connection Close } elseif { ([string tolower [HTTP::host]] ends_with "oldsite.com") && ([HTTP::path] ne "/") } { Forward traffic to HTTP pool pool NameOfMyHTTPpool80 } else { Generic HTTP to HTTPS redirect (ORIGINAL request Host and URI are retained) HTTP::respond 301 Location "https://[HTTP::host][HTTP::uri]" Connection Close } }- Sivaraj_6072_25
Nimbostratus
Hannes, This is perfect. but for some reason the second condition is not working. It is not sending traffic to the Pool. So I get connection reset error when I browse http://newsite.com/xyz. any idea? Thanks, Siva. - Sivaraj_6072_25
Nimbostratus
Small correction, i tried http://oldsite.com/xyz. And sites doesn't load at all. Finally it throws connection reset error. I verified the pool name and everything looks good. Thanks, Sivaraj. - Hannes_Rapp
Nimbostratus
Perhaps a conflict with another iRule? Do you see anything in /var/log/ltm ? Also, please provide output for your VS config: 'tmsh list ltm virtual YourVSName'
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