Forum Discussion

matt1985_299432's avatar
matt1985_299432
Icon for Nimbostratus rankNimbostratus
Sep 25, 2017

Redirect with URI rewrite setting lowercase URI and removing trailing /

Hi,

I am after some assistance. Essentially I have the requirement to change the URI which is passed to the server. i.e. client requests /content/content server gets /site/path/content/content so this appears to work OK I am using IRULE for the request and the response is being re-written with a rewrite profile (stream works too).

The site server side is all lower case I then had a request to change any case to lower as its sent to the server and also remove the trailing '/'. So the logic I have below works fine for this however if you request '/content/cOntent/' this is whats displayed in the browser address bar however the requirements are to display '/content/content'

So I am assuming a redirect is needed to achieve this however i have been have problems making the redirect work but still sending the additional path to the server. The working IRULE without the redirect is below. Is it possible I can get some assistance on how to combine a redirect with server URI rewrite?

when HTTP_REQUEST {
    if {[string tolower [HTTP::uri]] starts_with "/" }  {
        set sitepath [string tolower [HTTP::uri]] 
        HTTP::uri "/site/path$sitepath"
    }
    if { [HTTP::uri] ends_with "/" } {
        HTTP::uri [string trimright [HTTP::uri] "/"]
    }
}

Thanks in Advance.

4 Replies

  • Hi Guy,

    Try this iRule:
    when HTTP_REQUEST {
        if {[string tolower [HTTP::uri]] ne [HTTP::uri] && [string tolower [HTTP::uri]] starts_with "/content/"}  {
           HTTP::redirect "/site/path[HTTP::uri]"
        }
        if { [HTTP::uri] ends_with "/" } {
            HTTP::uri [string trimright [HTTP::uri] "/"]
        }
    }
    
  • Hi,

    this irule may cause some issues...

    if you want to remove ending /, how do you manage URI

    /
    ?

    And in HTTP, URI ending with a / is not the same as URI without /. are you sure of the application behavior if you remove it?

  • How about this:

    when HTTP_REQUEST {
       set sitepath [string tolower [HTTP::uri]]
       if { $sitepath ne [HTTP::uri] }{
           The lower case URI differs from HTTP::uri
           i.e. the HTTP::uri contains upper case characters
          HTTP::redirect $sitepath
       } elseif {[HTTP::uri] starts_with "/" }  {
            HTTP::uri "/site/path$sitepath"
       }
       if { [HTTP::uri] ends_with "/" } {
          HTTP::uri [string trimright [HTTP::uri] "/"]
       }
    }
    
  • Hi,

     

    you can try this code:

     

    when HTTP_REQUEST {
         Convert path to lowercase and remove first and last /. prepend a / (to prevent "/" removed)
         use path instead of uri to prevent query string blocking path transformation.
         the general behavior when converting to lower case is to keep query string case
        set new_path "/[string trim [string tolower [HTTP::path]] "/"]"
         Add /site/path if starting with /content
        if {$new_path starts_with "/content/"}  { set new_path "/site/path$new_uri" }
    
         Append query string if exists
        if {[HTTP::query] ne ""} {set new_uri $new_path?[HTTP::query]} else {set new_uri $new_path}
        if { $new_uri ne [HTTP::uri] } {
             if the new path is different to request path, redirect to new uri
            HTTP::redirect $new_uri
        }
    }