Simple URL redirect iRule

I am still very new to iRules, and I am trying to get the code correct for a simple URL redirect. The VS already has an iRule that will redirect traffic from port 80 to 443. Plus, I will need it to work with or without the leading www.

abc.com (www.abc.com) redirects to https://www.def.123.com/ghi

Any help with this will be greatly appreciated. Remember, I am still new to iRules.

Thanks!

Something like this should help:

when HTTP_REQUEST {
   if { (([HTTP::host] equals "abc.com") or ([HTTP::host] equals "www.abc.com")) } {
       HTTP::redirect "https://www.def.123.com/ghi"
     }
   }

Or you could simply use this line instead and this will catch all host types.

if {HTTP::host] contains "abc.com"}{

Hope this helps, N

A very basic (host-agnostic) redirect iRule might look something like this (applied to the port 80 VIP):

when HTTP_REQUEST {
    HTTP::redirect "https://www.def.123.com[HTTP::uri]"
}

Where [HTTP::uri] is the URI from the original request (if you need to keep that). This will take any request to the VIP and (blindly) redirect it. Or do you only need it to redirect if the requested host value is “abc.com” or “www.abc.com”?

when HTTP_REQUEST {
    if { [string tolower [HTTP::host]] ends_with ".abc.com" } {
        HTTP::redirect "https://www.def.123.com[HTTP::uri]"
    }
}

If this VIP services a single (DNS) resolved host name, then all requests should technically be for abc.com or www.abc.com, and you could skip the HTTP::host evaluation.

Does it make a difference if the incoming request is for HTTPS?

I need to redirect incomming HTTPS request to a different HTTPS URL.

“https://site1.com” needs to be redirected to “https://site2.com/page2”

Do I need to terminate the HTTPS session or can it be redirected to the site2 URL?

Thanks!

The above iRules will work for HTTPS requests as long as you have a client SSL profile applied to the VIP - terminating the SSL.

I am trying to do something similar to this but I can’t seem to get the re-direct to work. I’m trying to filter based on http_host and http_uri. Here is what I have currently:

when HTTP_REQUEST {
if { [HTTP::host] contains "sandboxcernercentral.com" }{
if { [HTTP::uri] starts_with "/audit-reports/admin/" }{ 
HTTP::redirect https://associates.sandboxcernercentral.com/sentinel/
}
} 
}

When I am trying to browse to https://sandboxcernercentral.com/audit-reports/admin/ it is still taking me to that page and not actually re-directing me to the /sentinel.

apologies for the unformatted post. I can’t seem to get the code formatting to work properly.

You can try this, and by no means am I an expert in this either. If this doesn’t work for you, you may want to post your question as a separate question so one of the experts can help.

when HTTP_REQUEST { if {([HTTP::uri] == “/audit-reports/admin/”) } { log local0. “PATH MATCH / URI: [HTTP::uri] HOST: [HTTP::host]" HTTP::redirect https://associates.sandboxcernercentral.com/sentinel } elseif { ! ([HTTP::host] contains “1800wxbrief”) } { log local0. "PATH MATCH / URI: [HTTP::uri] HOST: [HTTP::host]”

Please help me with the IRULE for the below requirement on urgent bases

Root URL : should redirect to non-ROOT URL Root URL : should redirect to non-ROOT URL

Please also note that direct access to non-ROOT URL, for example /BOE/BI should not be redirected.

======== URL should not redirect or change ========= URL should not redirect or change

if { [HTTP::uri] equals "/" } {
    HTTP::redirect "http://[HTTP::host]/SAM"
}

Hi Kevin,

I recommend to always redirect with relative URL instead of absolute when possible (same hostname and protocol).

if { [HTTP::uri] equals "/" } {
    HTTP::redirect "/SAM"
}

RFC2616 states that the Location header in an HTTP redirect needs to be an absolute URI.

https://tools.ietf.org/html/rfc2616section-14.30

That said, clearly all modern browsers support relative URIs, and indeed the standard that deprecates 2616 (7231) changed this wording to include relative URIs in the Location header.

But, and I’ll quote from a thread on Stackexchange,

“However, until the HTTPbis draft some day becomes the official standard and gets widely adopted, there will always be some new or obscure user agents that implement the current standard to the letter and only accept absolute URLs. Thus, the safe thing to do, for now, is to only use absolute URLs in Location headers…”

https://webmasters.stackexchange.com/questions/31274/what-are-the-consequences-for-using-relative-location-headers

And I’ll also provide here an interesting article on why relative URIs can be troublesome:

It is for these reasons that I generally always recommend an absolute URI in HTTP redirects.

Hi,

In your last link, there are 3 possible URI format:

  • Absolute URI including hostname and protocol
  • relative URI with absolute path (starting with /)
  • other relative URI (path of current URI is used to evaluate path of redirect)

I agree with you and the article author about problems with last solution.

but I think relative URI with absolute path doesn’t cause such issues and solve much more issues:

  • web server behind a reverse proxy with SSL offload
  • web server behind a reverse proxy with internal hostname
  • migration of web server name (new company name)

and even F5 uses relative URI in 302 redirect in APM (redirect to /my.policy, /vdesk/hangup.php3, landing URI, …)