Forum Discussion

SSell's avatar
SSell
Icon for Altostratus rankAltostratus
Sep 18, 2015

iRule to redirect to new url, but retain path after first location

I have been tasked with creating an irule to redirect a url to a new server. However the existing server will still have other urls still working on it. We need to retain everything in the URL after the first path.

 

old url: https://www.abc.com/APP/ redirect: https://app.def.com/

 

and example would be OLD URL: https://www.abc.com/APP/DD_CalendarDetail.aspx?id=16888&dt=2015-09-25 REDIRECT URL: https://app.def.com/DD_CalendarDetail.aspx?id=16888&dt=2015-09-25

 

Would the following both redirect, and maintain the path after the /APP/ ?

 

when HTTP_REQUEST { if { [HTTP::host] equals "www.abc.com" and [HTTP::uri] starts_with "/APP/" } { HTTP::redirect "https://app.def.com/"

 

In addition, am I correct in the assumption that any urls not matching will just ignore the iRule and process normally?

 

14 Replies

  • try this

    when HTTP_REQUEST { 
        if { [HTTP::host] equals "www.abc.com" and [HTTP::uri] starts_with "/APP/" } { 
            HTTP::redirect "https://app.def.com/[string map {/APP/ /} [HTTP::uri]]"
        }
    }
    
  • For a permanent redirect you would use this to do a 301:

    when HTTP_REQUEST { 
        if { [string tolower [HTTP::host]] equals "www.abc.com" and [string tolower [HTTP::uri]] starts_with "/app/" } { 
            HTTP::respond 301 noserver Location "https://app.def.com/[string map {/app/ /} [string tolower [HTTP::uri]]]"
        }
    }
    
    • SSell's avatar
      SSell
      Icon for Altostratus rankAltostratus
      We don't have anyway to test this until actual implementation. I was going to leave the question open until we implement and test which is scheduled for next Tuesday. Thanks for your help
    • Brad_Parker's avatar
      Brad_Parker
      Icon for Cirrus rankCirrus
      You probably want to add string tolower to prevent case mis-match compares. I update above.
  • For a permanent redirect you would use this to do a 301:

    when HTTP_REQUEST { 
        if { [string tolower [HTTP::host]] equals "www.abc.com" and [string tolower [HTTP::uri]] starts_with "/app/" } { 
            HTTP::respond 301 noserver Location "https://app.def.com/[string map {/app/ /} [string tolower [HTTP::uri]]]"
        }
    }
    
    • SSell's avatar
      SSell
      Icon for Altostratus rankAltostratus
      We don't have anyway to test this until actual implementation. I was going to leave the question open until we implement and test which is scheduled for next Tuesday. Thanks for your help
    • Brad_Parker_139's avatar
      Brad_Parker_139
      Icon for Nacreous rankNacreous
      You probably want to add string tolower to prevent case mis-match compares. I update above.
  • Hello

     

    I have a similar issue - where the redirection should retain path after the second location.

    when HTTP_REQUEST {

    # Do redirects.

    if { ([string tolower [HTTP::host]] equals "abcshare") and ([HTTP::uri] starts_with "/site/app") } {

    HTTP::respond 301 Location "https://app.abcshare.com[HTTP::uri]"

    }

    elseif { [string tolower [HTTP::host]] equals "abcshare" and ([HTTP::uri] starts_with "/") } {

    HTTP::respond 301 Location "https://abcshare.com/"

    }

    }