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

Filter by:
  • Solution
  • Technology
Answers

iRule for redirection with masking for a specific URI only.

Folks,

We received some redirect requests for a very particular scenario and based on that I have come up with a iRule which I want to get some additional comments on:

The redirect requests are as below:

1) If an request is made for https://www.abc.com/manual/* the request needs to forward to https://www.newsite.com/support/txt/docs/. 2) The above redirects must be masked i.e. the end user should not know where is he going to. 3) This applies to URI's which start with "/manual" in the URI. If the keyword "manual" is seen at any other place no action should be taken. 4) The wild character "" indicates this could be any string after "/manual". 5) The redirects would also need to be https. 6) Other than this no other URI needs to be redirected.

How should the below iRule look for this request:

when HTTP_REQUEST { if { [HTTP:uri] starts_with "/manual/" } HTTP::header replace "Host" "www.newsite.com"; HTTP::uri "/support/txt/docs/" }

Thanks!! N.

0
Rate this Question

Answers to this Question

placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

Have a look at this answer:

https://devcentral.f5.com/questions/uri-translation-vs-httpredirect-irule-52327

I modified your iRule, you will have to test:

when HTTP_REQUEST {
    if { [HTTP::uri] starts_with "/manual/" } {
        HTTP::header replace "Host" "www.newsite.com"
        set uri [string range [HTTP::uri] 8 end]
        HTTP::uri "/support/txt/docs/$uri" 
    }
}

The logic is correct, but you had multiple syntax errors. Also, you need to send the remaining of the URI to the server.

0
Comments on this Answer
Comment made 16-Jul-2018 by N. 249

what will the line "set uri [string range [HTTP::uri] 8 end]" do? I added the "/manual" as an example only and that actual URI is something else.

0
Comment made 17-Jul-2018 by Leonardo Souza 3142

The URI will be something like (maybe have a query string ?a=b as well):

/manual/def/page.html

You want to change to:

/support/txt/docs/def/page.html

So you need to add the end of the URI to the new URI.

This will remove the first 8 characters (0 to 7) referent to /manual/:

set uri [string range [HTTP::uri] 8 end]

0
Comment made 19-Jul-2018 by N. 249

I tested this but this does not seem to work. One observation that I have is that there is a file name at the end at some place.

i.e. https://www.abc.com/manual/help.pdf

redirect with masking to

https://www.newsite.com/support/txt/docs/help.pdf.

Could the file name make a difference?

0
Comment made 19-Jul-2018 by Leonardo Souza 3142

Try this version with log commands:

when HTTP_REQUEST {
    if { [HTTP::uri] starts_with "/manual/" } {
        log local0. "Original host: [HTTP::host]"
        HTTP::header replace "Host" "www.newsite.com"
        log local0. "New host: [HTTP::host]"
        log local0. "Original URI: [HTTP::uri]"
        set uri [string range [HTTP::uri] 8 end]
        log local0. "URI variable: $uri"
        HTTP::uri "/support/txt/docs/$uri" 
        log local0. "New URI: [HTTP::uri]"
    }
}       

You will see the logs in /var/log/ltm. Let me know what you get in the logs.

1
Comment made 20-Jul-2018 by N. 249

The original host and the new host is coming out to be the same.

Could this be due to a LTM version? we are running the 11.4

0
Comment made 20-Jul-2018 by N. 249

One more interesting observation:

If I remove the first log line i.e. log local0. "Original host: [HTTP::host]" the correct host is seen. :)

this is something foxing me as of now.

So essentially, with the first log line removed all the correct host and URI is seen. While going through a few more articles I see hints of response headers needing to be modified for such requests...

0