Forum Discussion

Silver_back's avatar
Silver_back
Icon for Altostratus rankAltostratus
Feb 08, 2022

URL redirect with path

I am trying to create a redirect irule but is not a standard redirect. It basically needs to take the URL + path and redirect it to another website + the path of the old URL. E.g.

www.abc.com/path1/subpath2/ -> https://www.def.com/path1/subpath1/
www.abc.com/path4/subpath5/ -> https://www.def.com/path4/subpath5/
www.abc.com/ - > https://www.def.com/

The Url is not always the same therefore it needs to be flexible. Basically apply everything after the "/" to the new URL.
In a situation where there is no path it just execute a normal redirect. below is my attempt which isn't working, not sure why.

when HTTP_REQUEST {

if { ([string tolower [HTTP::host]] equals "www.abc.com") and ([HTTP::uri] starts_with "/") } {

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

}else {

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

}

}

 

5 Replies

  • A couple comments/questions...

    • All URLs start with "/", so as long as the host is www.abc.com, you'll never get to the else.
    • There is no need to string tolower the host, that is case insensitive according to the RFC.
    • Do you want any redirects if the host is not www.abc.com? If it's not, there is no additional logic to handle that.

    That said, assuming you don't need any redirects for the non-matching host, here's how I'd write that:

     

    when HTTP_REQUEST {
        if { ([HTTP::host] eq "www.abc.com") and not([HTTP::uri] eq "/") } {
            HTTP::respond 301 Location "http://jrahmtest.local[HTTP::uri]"
            event disable all
        } elseif { [HTTP::host] eq "www.abc.com" } {
            HTTP::respond 301 Location "http://jrahmtest.local/"
            event disable all
        }
    }

     

    Test results:

     

    • JRahm's avatar
      JRahm
      Icon for Admin rankAdmin

      do you have a dump capturing what the BIG-IP is returning? you could also capture the redirect in HTTP_RESPONSE_RELEASE I think. Its working on my end, not sure what the issue is, will need to see what you're seeing.

      • Silver_back's avatar
        Silver_back
        Icon for Altostratus rankAltostratus

        We were unable to get the iRule to work, therefore we sent the traffic to an apache server and on the server, we were able to configure the redirect to the right path. Thank you for your help.