cancel
Showing results for 
Search instead for 
Did you mean: 
Login & Join the DevCentral Connects Group to watch the Recorded LiveStream (May 12) on Basic iControl Security - show notes included.

URL redirect with path

Silver_back
Altostratus
Altostratus

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 5

JRahm
Community Manager
Community Manager

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:

test1.pngtest2.png

 

Silver_back
Altostratus
Altostratus

jRahm thank you for your reply/suggestion. Unfortunately, it didn't work properly.

www.abc.com redirect to www.def.com work fine
However,
www.abc.com/somepath/someotherpath/ redirect to www.def.com/somepath/someotherpath/ did not work. It just redirects without the path.

JRahm
Community Manager
Community Manager

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.

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.

JRahm
Community Manager
Community Manager

Glad you found a solution! If you want to keep working it happy to help, this is totally doable with an iRule, we must be missing a piece of logic.