Forum Discussion

newf5learner_13's avatar
newf5learner_13
Icon for Nimbostratus rankNimbostratus
May 18, 2016

irule to redirect to specific uri..

Hi All,

I need a irule that helps me to redirect users to specific resources / folders on the servers.

I have three servers, all of them hosting sites

https://site1.company.com   https://site2company.com    https://site3.company.com

All the 3 sites are pointed to load balanced VIP 10.20.20.200

I want a irule that helps me to serve the redirection based on hostname.

if a user tried to access

https://site1.company.com
it should redirect him to
https://site1.company.com/USERSITE1/

similarly if a user tries to access

https://site2.company.com
, it should redirect him to
https://site2.company.com/MY_USER_SITE/

and thirdly if a user tries to access

https://site3.company.com
, it should redirect him to
https://site3.company.com/SITE3/

I tried to use the below url, but its not working as expected.

when HTTP_REQUEST {
set host [string tolower [HTTP::host]]
if { $host contains "site1.company.com"} {
    HTTP::redirect "https://site1.company.com/USERSITE1"
} elseif { $host contains "site2.company.com"} {
    HTTP::redirect "https://site2.company.com/MYUSERSITE"
} elseif { $host contains "site3.company.com"} {
    HTTP::redirect "https://site3.company.com/SITE3"
}
}

thanks in advance for your help.

7 Replies

  • Hi,

    you redirected all URI, so the redirected may be looping.

    when HTTP_REQUEST {
        set host [string tolower [HTTP::host]]
        if HTTP::uri equals "/" { 
            switch -glob [string tolower [HTTP::host]] {
                "site1.company.com" {HTTP::redirect "/USERSITE1"}
                "site2.company.com" {HTTP::redirect "/USERSITE2"}
                "site3.company.com" {HTTP::redirect "/USERSITE3"}
            }
        }
    }
    
  • Its throwing below error. Sorry, I'm not much familiar to correct this error. 😞

    01070151:3: Rule [/Common/new_pmp_https_uri_locator] error: /Common/new_pmp_https_uri_locator:3: error: [parse error: PARSE syntax 74 {syntax error in expression "HTTP::uri": variable references require preceding $}][HTTP::uri]
    /Common/new_pmp_https_uri_locator:3: error: [undefined procedure: equals][equals]
    /Common/new_pmp_https_uri_locator:3: error: [undefined procedure: /][/]
    /Common/new_pmp_https_uri_locator:3: error: [undefined procedure: 
    
  • Sorry, I forgot brackets

    when HTTP_REQUEST {
        if {[HTTP::uri] equals "/"} { 
            switch -glob [string tolower [HTTP::host]] {
                "site1.company.com" {HTTP::redirect "/USERSITE1"}
                "site2.company.com" {HTTP::redirect "/USERSITE2"}
                "site3.company.com" {HTTP::redirect "/USERSITE3"}
            }
        }
    }
    
  • I tried this irule.. I irule is executing without any problem, but as its referring to 'host' we are seeing too many redirections error. I'm getting this error on my browser 'The site1.company.com page isn’t working site1.company.com redirected you too many times.'

    when HTTP_REQUEST {
      if {([HTTP::host] contains "site1.company.com")} {
        HTTP::redirect"https://site1.company.com/USERSITE1"
      }
    elseif {([HTTP::host] contains "site2.company.com")} {
        HTTP::redirect "https://site2.company.com/USERSITE2"
      }
    elseif {([HTTP::host] contains "site3.company.com")} {
        HTTP::redirect "https://site3.company.com/USERSITE3"
      }
    }
    

    Please suggest how to block these too many re-directions or suggest me with a better irule. thanks.

  • I tried this irule.. However, I'm getting this error on my browser 'The site1.company.com page isn’t working site1.company.com redirected you too many times.'

    when HTTP_REQUEST {
      if {([HTTP::host] contains "site1.company.com")} {
        HTTP::redirect"https://site1.company.com/USERSITE1"
      }
    elseif {([HTTP::host] contains "site2.company.com")} {
        HTTP::redirect "https://site2.company.com/USERSITE2"
      }
    elseif {([HTTP::host] contains "site3.company.com")} {
        HTTP::redirect "https://site3.company.com/USERSITE3"
      }
    }
    

    Please suggest how to block these too many re-directions or suggest me with a better irule. thanks.

  • this irule is working..NOTE: the uri was missing at the end of redirected url

    "site1.company.com" {HTTP::redirect "/USERSITE1[HTTP::uri]"}

    thanks for your help.

    Working irule:

    when HTTP_REQUEST {
        if {[HTTP::uri] equals "/"} { 
            switch -glob [string tolower [HTTP::host]] {
                "site1.company.com" {HTTP::redirect "/USERSITE1[HTTP::uri]"}
                "site2.company.com" {HTTP::redirect "/USERSITE2[HTTP::uri]"}
                "site3.company.com" {HTTP::redirect "/USERSITE3[HTTP::uri]"}
            }
        }
    }
    
  • OK,

    [HTTP::uri] is /, so do not append [HTTP::uri] but / 🙂

    The following rule may work:

    when HTTP_REQUEST {
        if {[HTTP::uri] equals "/"} { 
            switch -glob [string tolower [HTTP::host]] {
                "site1.company.com" {HTTP::redirect "/USERSITE1/"}
                "site2.company.com" {HTTP::redirect "/USERSITE2/"}
                "site3.company.com" {HTTP::redirect "/USERSITE3/"}
            }
        }
    }
    

    This is the same as mine, except the end of the URI which is /