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

Filter by:
  • Solution
  • Technology
Answers

Redirect host and path but keep query string on redirect

I need to redirect a request to a new host and path but keep the query string the same. Can someone help me with the best way of doing this?

If request contains “https://olddomain.com/SelfServ/WebContactResponse.aspx?
then Redirect to https://new-domain.com/my-account/w...spx? string>

 

Will this work?

when HTTP_REQUEST {
    if { [HTTP::path] equals "/SelfServ/WebContactResponse.aspx"} {
        HTTP::redirect "
https://new-domain.com/my-account/w....aspx?$uri"
    }
}

Thanks in advance for the help!

Dave

1
Rate this Question

Answers to this Question

placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
Hi Davean,

You can use the <a href="https://devcentral.f5.com/wiki/iRules.http__query.ashx">[HTTP::query]</a> command to have it added onto the end.

NOTE: The HTTP::query value does not include the "?".

Try this:
 
when HTTP_REQUEST {
	if { [string tolower [HTTP::path]] equals "/selfserv/webcontactresponse.aspx" } {
		HTTP::redirect "https://new-domain.com/my-account/web-contact-response.aspx?[HTTP::query]"
    }
}
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
Thank you Michael! If the path will be changed in the redirect why do I need the "string tolower" ?
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
The "string tolower" takes the value of the [HTTP::path] and puts everthing in lower case.

So if someone entered "/SelfServ/WebContactResponse.aspx" or "/SELFSERV/WEBCONTRACTRESPONSE.ASPX" the value would be put into all lower case "/selfserv/webcontactresponse.aspx" (a known state) for an accurate comparison to trigger the event.

It does not change the actual value that will be sent to the server. Since the URI Value of a URL is case sensitive and doing so may cause your application to fail.
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

There's a problem with this, it will redirect to: https://new-domain.com/my-account/web-contact-response.aspx? if there is no query.
 

Here's what I do:


 
 
when HTTP_REQUEST {
if { [string tolower [HTTP::path]] equals "/selfserv/webcontactresponse.aspx" } {
if { [HTTP::query] ne "" }{
HTTP::redirect "https://new-domain.com/my-account/web-contact-response.aspx?[HTTP::query]"
else {
HTTP::redirect "https://new-domain.com/my-account/web-contact-response.aspx"
    }
}
 
I came to this thread wondering if the performance was the same to do:
 
when HTTP_REQUEST {
if { [string tolower [HTTP::uri]] equals "/selfserv/webcontactresponse.aspx" } {
HTTP::redirect "https://new-domain.com/my-account/web-contact-response.aspx"
elseif { [string tolower [HTTP::path]] equals "/selfserv/webcontactresponse.aspx" } {
HTTP::redirect "https://new-domain.com/my-account/web-contact-response.aspx?[HTTP::query]"
    }
}
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

Technically it should not matter if there's a superfluous "?" at the end of the URL.

0