Learn F5 Technologies, Get Answers & Share Community Solutions Join DevCentral

Filter by:
  • Solution
  • Technology
Answers

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!

6
Rate this Question
Comments on this Question
Comment made 28-Apr-2017 by somashekar 0

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

Root URL : http://sapbobjtst1.network.qut/ should redirect to non-ROOT URL http://sapbobtst1.network.qut/SAM Root URL : http://sapbobj.network.qut/ should redirect to non-ROOT URL http://sapbobj.network.qut/SAM

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

http://sapbobj.network.qut/BOE/BI ======== URL should not redirect or change http://sapbobjtst1.network.qut/BOE/BI ========= URL should not redirect or change

0
Comment made 28-Apr-2017 by Kevin Stewart
if { [HTTP::uri] equals "/" } {
    HTTP::redirect "http://[HTTP::host]/SAM"
}
0
Comment made 29-Apr-2017 by Stanislas Piron 10481

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"
}
0
Comment made 30-Apr-2017 by Kevin Stewart

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

https://tools.ietf.org/html/rfc2616#section-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:

https://yoast.com/dev-blog/relative-urls-issues/

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

0
Comment made 30-Apr-2017 by Stanislas Piron 10481

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, ...)

0

Answers to this Question

placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

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.

3
Comments on this Answer
Comment made 17-Sep-2013 by Chris 197
Thank you! I used the first one that you have posted on here, and it works perfectly for me at this point. Thank you for your time! Chris
0
Comment made 18-Sep-2015 by Root44 389
How I can add it to virtual server? (An iRule)
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

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!

1
Comments on this Answer
Comment made 18-Sep-2013 by Kevin Stewart
The above iRules will work for HTTPS requests as long as you have a client SSL profile applied to the VIP - terminating the SSL.
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

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

0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

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 <a class="externalLink" href="https://associates.sandboxcernercentral.com/sentinel/" rel=" nofollow" target="_blank">https://associates.sandboxcernercentral.com/sentinel/</a> } } }

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.

0
Comments on this Answer
Comment made 03-Mar-2015 by brad9iner 58
apologies for the unformatted post. I can't seem to get the code formatting to work properly.
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

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]"

0