Anchor Link Redirect

Code is community submitted, community supported, and recognized as ‘Use At Your Own Risk’.

Short Description

This code snippet can be used to interact with links that contain a hash sign (anchor link).

Problem solved by this Code Snippet

Links in websites that contain a hash sign will not be send from the browser to the server if they are used. That makes it impossible to match them in an iRule, because the F5 will never receive them. This code snippet injects a little bit of javascript and will redirect the browser to the F5 when a user clicks on a link that contains a hash sign.

How to use this Code Snippet

Attach the iRule to a server that has a STREAM-profile enabled. Modify the iRule to your needs.

Code Snippet Meta Information

  1. Version: 1.0
  2. Coding Language: TCL
when HTTP_REQUEST {
    # Disable the stream filter by default   
    STREAM::disable 
 
    # LTM does not uncompress response content, so if the server has compression enabled
    # and it cannot be disabled on the server, we can prevent the server from
    # sending a compressed response by removing the compression offerings from the client
    HTTP::header remove "Accept-Encoding"
    
    if { [HTTP::uri] starts_with "/f5/anchor_link_redirect" } {
        set href [b64decode [URI::query [HTTP::uri] href]]
        HTTP::respond 200 content "<html><head><title>Anchor Link Redirect</title></head><body>User clicked on link that contains a hash sign: $href</body></html>"
    }
}
	
when HTTP_RESPONSE {
    if { ([HTTP::header "Content-Type"] starts_with "text/html") } { 
        STREAM::expression {@</title>@</title>
    <script>
    document.addEventListener(`click`, e => {
      const origin = e.target.closest(`a`);

      if (origin && origin.href.indexOf('#') > -1) {
        const base64_href = btoa(origin.href);
        window.location.href = '/f5/anchor_link_redirect?href=' + base64_href;
      }
    });
    </script>@}
        STREAM::enable
    }
}

 

 

Updated Oct 29, 2023
Version 2.0

Was this article helpful?

No CommentsBe the first to comment