Forum Discussion

heskez_36146's avatar
heskez_36146
Icon for Nimbostratus rankNimbostratus
Nov 13, 2013

Redirection multiple URLS

Hi,

We've two websites www.x.com/GoWest and www.x.com/GoSouth

We'd like an Irule which redirects to:

GoWest -> www.y.com/Gowest and GoSouth -> www.y.com/GoSouth

Therefore We've build an irule:

 when HTTP_REQUEST
{

set RequestedPath [string tolower [HTTP::path]]

switch $RequestedPath
   {


      "/GoWest"
      {
         HTTP::redirect "https://www.y.com/GoWest"
      }
      "/GoSouth"
      {
         HTTP::redirect "https://www.y.com/GoSouth"
      }

    }

if { $RequestedPath contains "/GoWest" }
{
      HTTP::redirect "https://https://www.y.com/GoWest"
}
if { $RequestedPath contains "/GoSouth" }
{
HTTP::redirect "https://https://www.y.com/GoSouth"
}
else
{
HTTP::redirect "https://www.y.com"
TCP::close
}
}

This is not working as expected though, it's always going to West and never to south?

  • I would just do the switch statment and not the switch and the if statment. The redirects in the if statments should not be https://https. The problem with the switch statment is you are doing a string tolower but then are matching on a string with Caps. It would also be good in troubleshooting to add a log statement showing what you are matching.

    when HTTP_REQUEST {
    set RequestedPath [string tolower [HTTP::path]]
    log local0. "RequestedPath = $Requestedpath" 
    
    switch $RequestedPath
    {
    
    
      "/gowest"
      {
         HTTP::redirect "https://www.y.com/GoWest"
      }
      "/gosouth"
      {
         HTTP::redirect "https://www.y.com/GoSouth"
      }
    
    }
    
    }
    
  • Richard__Harlan's avatar
    Richard__Harlan
    Historic F5 Account

    I would just do the switch statment and not the switch and the if statment. The redirects in the if statments should not be https://https. The problem with the switch statment is you are doing a string tolower but then are matching on a string with Caps. It would also be good in troubleshooting to add a log statement showing what you are matching.

    when HTTP_REQUEST {
    set RequestedPath [string tolower [HTTP::path]]
    log local0. "RequestedPath = $Requestedpath" 
    
    switch $RequestedPath
    {
    
    
      "/gowest"
      {
         HTTP::redirect "https://www.y.com/GoWest"
      }
      "/gosouth"
      {
         HTTP::redirect "https://www.y.com/GoSouth"
      }
    
    }
    
    }
    
  • This should. You were lowering the path, but still had the capitalisation in your switch cases. No need for the if statements, they just duplicated what the switch did.

    when HTTP_REQUEST {
    
    set RequestedPath [string tolower [HTTP::path]]
    
    switch $RequestedPath {
    
          "/gowest" {
             HTTP::redirect "https://www.y.com/GoWest"
          }
          "/gosouth" {
             HTTP::redirect "https://www.y.com/GoSouth"
          }
          default {
             HTTP::redirect "https://www.y.com"
        }
     }
    }
    
  • Thanks both, I've changed everything to lower case. But unfortunately some problem still arises. For the switch statement, we'd also want to redirect www.x.com to www.y.com

     

  • Richard__Harlan's avatar
    Richard__Harlan
    Historic F5 Account

    If you want to redirect based on the hostname it will have to happen outside the switch statement as it only looks at the URI. Did you put the log statement into the iRule if so check the /var/log/ltm and make sure the iRule is see what you expect it to see.

     

  • I'm getting this error message:

     

    TCL error: /Common/www.y.com_Redirect.V2 - can't read "Requestedpath": no such variable while executing "log local0. "RequestedPath = $Requestedpath""

     

  • Richard__Harlan's avatar
    Richard__Harlan
    Historic F5 Account

    Look like I left out the Capital P in $RequestedPath. Once that is changed it should work. Sorry about that.

     

  • I see thanks :) I'm not redirected to the correct site I'm getting the famous: "Internet explorer cannot display the webpage" Over and over..

     

    • Richard__Harlan's avatar
      Richard__Harlan
      Historic F5 Account
      Most likely you are getting a 404/500, IE turns small error pages into the page you are seeing. The ltm logs will show what the BigIP is seeing and what it should pick in the switch statement. If you take a HTTPwatch or fiddler you will see the error statement from the server, or you can try the same site with firefox and it will show the real error.
  • I've got it running. This is what I end up with:

    when HTTP_REQUEST {
       switch -glob [string tolower [HTTP::path]] {
          "*gosouth*" {
              add the redirect link ie "www.xyz.com"
             HTTP::redirect "https://www.y.com/GoSouth"
          }
          default {
              add the default action you prefer ie "www.def.com"
             HTTP::redirect "https://www.y.com/GoWest"
          }
       }
    }