Forum Discussion

Richard_77286's avatar
Richard_77286
Icon for Nimbostratus rankNimbostratus
Mar 17, 2017

Help with iRule redirect

I have a request from our dev team to redirect https://www.ourwebsite.com/agentlanguagelocator to https://www.ourwebsite.com/apex_extprd/f?p=119:1. Dev team would like users to use /agentlanguagelocator since it's easier to remember.

 

I tried the iRule below but it's not working.

 

when HTTP_REQUEST { if { [string tolower [HTTP::uri]] starts_with "/agentlanguatelocator" } { HTTP::uri "/apex_extprd/f?p=119:1"

 

} }

 

Am I missing something? Any help or comment is appreciated.

 

Richard

 

  • Hello Richard,

    Based on your iRule, you are not performing a redirect, you are only changing the URI before it is passed to the server. This is completely transparent to the user/browser.

    If your Dev team has requested a redirect, it should probably look closer to this:

     

    when HTTP_REQUEST {
        if {[string tolower [HTTP::uri]] eq "/agentlanguagelocator"} {
            HTTP::respond 302 Location "/apex_extprd/f?p=[URI::encode 119:1]"
        }
    }
    

    Notes

     

    1. Notice the use of HTTP::respond, this is not required as HTTP::redirect will perform the same function. My preference is to use HTTP::respond so the status code is clear at all times and to any readers of the iRule.
    2. The match is only for the URI and it's a case-insensitive exact match. Your requirements may vary.
    3. If the virtual server handles multiple domain names that don't all have this desired behavior, you should include a domain name match as well.
    4. The Location header is /apex_extprd/f?p=119:1 and not the full URL. This is a relative redirect directing the browser to use the same scheme (e.g. http / https) and domain name as the original request, but redirect to a different path. If you need the whole URL, that should be changed.
    5. The value of query parameter p is wrapped with URI::encode. This is because the colon is a reserved URI character and may cause problems if not encoded. You may also directly type 119%3A1 which should be the result of using URI::encode and translates to 119:1.
  • Did you use curl to check the response from the F5 ?

     

    curl -IL https://www.ourwebsite.com/agentlanguagelocator

     

     

  • Hello Richard,

    Based on your iRule, you are not performing a redirect, you are only changing the URI before it is passed to the server. This is completely transparent to the user/browser.

    If your Dev team has requested a redirect, it should probably look closer to this:

     

    when HTTP_REQUEST {
        if {[string tolower [HTTP::uri]] eq "/agentlanguagelocator"} {
            HTTP::respond 302 Location "/apex_extprd/f?p=[URI::encode 119:1]"
        }
    }
    

    Notes

     

    1. Notice the use of HTTP::respond, this is not required as HTTP::redirect will perform the same function. My preference is to use HTTP::respond so the status code is clear at all times and to any readers of the iRule.
    2. The match is only for the URI and it's a case-insensitive exact match. Your requirements may vary.
    3. If the virtual server handles multiple domain names that don't all have this desired behavior, you should include a domain name match as well.
    4. The Location header is /apex_extprd/f?p=119:1 and not the full URL. This is a relative redirect directing the browser to use the same scheme (e.g. http / https) and domain name as the original request, but redirect to a different path. If you need the whole URL, that should be changed.
    5. The value of query parameter p is wrapped with URI::encode. This is because the colon is a reserved URI character and may cause problems if not encoded. You may also directly type 119%3A1 which should be the result of using URI::encode and translates to 119:1.
    • Richard_77286's avatar
      Richard_77286
      Icon for Nimbostratus rankNimbostratus

      Hi Jeremy, Thank you for your response! You are correct, it's not a redirect. It's like creating an alias. The rule you provided above worked. And thank you for taking the time in writing the notes on the rule. Thanks again!