Forum Discussion

RiverFish's avatar
RiverFish
Icon for Altostratus rankAltostratus
Jan 05, 2021

Replace path in redirect

Expectations:

using the old hostname (oldhostname.com) will redirect to the new hostname (newhostname.com)

using / (nothing after .com) will redirect to /newpath

using /oldpath/foo will redirect to /newpath/foo

The issue is the last part. If you do something like this...

when HTTP_REQUEST {
if { [string tolower [HTTP::uri]] starts_with "/oldpath" } {
    HTTP::redirect "/newpath[HTTP::uri]" 
   }
 }

It grabs the oldpath, so it looks like this /newpath/oldpath. Please help if you can

  • Hello,

     

    You can try to re-create your iRule by referring below article.

     

    https://support.f5.com/csp/article/K48922683

  • Hello,

     

    You can try to re-create your iRule by referring below article.

     

    https://support.f5.com/csp/article/K48922683

    • RiverFish's avatar
      RiverFish
      Icon for Altostratus rankAltostratus

      Mayur, we eventually found that article and were able to come up with the irule below which got us working. The switch statement is just some tweaks that are custom for our particular site. Thanks!

      when HTTP_REQUEST {
        if { [HTTP::host] contains "oldhostname.com" } {
          set newhost "newhostname.com"
          set newpath [string map {"/oldpath/" "/newpath/"} [HTTP::uri]]
          HTTP::redirect "https://$newhost$newpath"
          return
        } else {
            switch [HTTP::uri] {
              "/" -
              "/oldpath" -
              "/oldpath/" -
              "/oldpath/login/" -
              "/newpath/login/" { HTTP::redirect "https://[HTTP::host]/newpath/login" }
              default {}
            }
            return
          }
      }