Forum Discussion

Neil_66348's avatar
Neil_66348
Icon for Nimbostratus rankNimbostratus
Aug 15, 2013

HTTP Cookie Redirect based upon cookie value and URL

Hi Guys,

Have an internal APP with needs language support now... Basically , redirect to a different URL based upon a language cookie set on the web server and the URL asked for .

The below is basically pretty wrong , and help really grateful , had a good search through the forums but can't find that much which is in the direction I need.

when HTTP_REQUEST {

 Try to select a pool based on the http cookie containing pool information

if { [HTTP::cookie exists "LANG"] and [HTTP::cookie value "LANG"] contains "EN"} {
xxxx(and website is www.website.com or website.com (the root "/") then redirect to --->www.website.com/page.apsx?lang=en)
}
elseif { [HTTP::cookie exists "LANG"] and [HTTP::cookie value "LANG"] contains "ES"} {
xxxx(and website is www.website.com or website.com (the root "/") then redirect to --->www.website.com/page.apsx?lang=ES)
}
elseif { [HTTP::cookie exists "LANG"] and [HTTP::cookie value "LANG"] contains "DE"} {
xxxx(and website is www.website.com or website.com (the root "/") then redirect to --->www.website.com/page.apsx?lang=DE)
}
else {
(no nothing)
}
}

Many Thanks Neil

4 Replies

  • nathe's avatar
    nathe
    Icon for Cirrocumulus rankCirrocumulus

    Try this Neil, no expert at iRules but enjoy giving them a go and happy to help if I can.

    when HTTP_REQUEST {
    if { [HTTP::cookie exists "LANG"] } {
      switch [HTTP::host] {
        "www.website.com" -
        "website.com" {
           switch [HTTP::cookie value "LANG"] {
           EN { HTTP::redirect "www.website.com/page.apsx?lang=en" }
           ES { HTTP::redirect "www.website.com/page.apsx?lang=ES" }
           DE { HTTP::redirect "www.website.com/page.apsx?lang=DE" }
          }
        }
       }
      }
    }
    
  • Hi Nathan ,

    Thanks for the reply.

    The HTTP cookie part works perfectly , the redirection works. However it appears to fail as the rule applies to any URI , eg www.website.com/test/page it will redirect to "www.website.com/page.apsx?lang=en" , so you can't browse to any page deeper....

    I tried the HTTP:HOST equals - but that doesn't appear to work. The request should only fire when for the root page....

    when HTTP_REQUEST {
    if { [HTTP::cookie exists "LANG"] } {
      switch [HTTP::host] equals {
        "www.website.com" -
        "website.com" {
           switch [HTTP::cookie value "LANG"] {
           EN { HTTP::redirect "www.website.com/page.apsx?lang=en" }
           ES { HTTP::redirect "www.website.com/page.apsx?lang=ES" }
           DE { HTTP::redirect "www.website.com/page.apsx?lang=DE" }
          }
        }
       }
      }
    }
    
  • Neil, give this a shot

    when HTTP_REQUEST {
    if { [HTTP::cookie exists "LANG"] } {
      switch "[HTTP::host][HTTP::uri]" {
        "www.website.com/" -
        "website.com/" {
           switch [HTTP::cookie value "LANG"] {
           EN { HTTP::redirect "www.website.com/page.apsx?lang=en" }
           ES { HTTP::redirect "www.website.com/page.apsx?lang=ES" }
           DE { HTTP::redirect "www.website.com/page.apsx?lang=DE" }
          }
        }
       }
      }
    }
    

    This will only work for URI's of "/" though. If you want to include a index.html (or similar default page for your system), you'll have to add different values to the comparisons for each host like "www.website.com/index.html".

    -Joe

  • Hi Joe ,

     

    Works great , some interesting caching issues with some elements integrated into Akamai.

     

    Many thanks guys.

     

    Neil