Forum Discussion

Sivaraj_6072_25's avatar
Sivaraj_6072_25
Icon for Nimbostratus rankNimbostratus
Apr 01, 2016

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

  • 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's avatar
      Sivaraj_6072_25
      Icon for Nimbostratus rankNimbostratus
      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.
  • 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's avatar
      Sivaraj_6072_25
      Icon for Nimbostratus rankNimbostratus
      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.
  • 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's avatar
      Sivaraj_6072_25
      Icon for Nimbostratus rankNimbostratus
      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's avatar
      Sivaraj_6072_25
      Icon for Nimbostratus rankNimbostratus
      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's avatar
      Hannes_Rapp_162
      Icon for Nacreous rankNacreous
      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'
  • 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's avatar
      Sivaraj_6072_25
      Icon for Nimbostratus rankNimbostratus
      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's avatar
      Sivaraj_6072_25
      Icon for Nimbostratus rankNimbostratus
      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's avatar
      Hannes_Rapp
      Icon for Nimbostratus rankNimbostratus
      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'