Forum Discussion

Wolf46_144992's avatar
Wolf46_144992
Icon for Nimbostratus rankNimbostratus
Jun 16, 2014

Append text to a URI

Hi Guys,

 

I am currently trying to append a "/" at the end of a URI provided that the user himself did not include this in the first place.

 

I am currently using the below iRule:

 

when HTTP_REQUEST { if { [HTTP::path] equals "/test" } { HTTP::redirect "//sample.domain.com/test/"} }

 

However I would like to make this more generic and be able to append the "/" at the end of every URI, provided that the user did not include this him/herself.

 

5 Replies

  • nathe's avatar
    nathe
    Icon for Cirrocumulus rankCirrocumulus

    What about this as your if statement?

    if { not [HTTP::uri] ends_with "/" } {

    Hope this helps,

    N

  • Ok this will do what you need. You do not need to specify the entire host as the browser will use the existing site name if you only supply just the URI in the redirect.

    when HTTP_REQUEST {
      if {!([HTTP::path] ends_with "/")} { 
        HTTP::path "[HTTP::path]/"
        HTTP::redirect [HTTP::uri]
      }
    }
    
  • If any of the below posts have provided a solution to your issue, please indicate so by clicking the tick to the left of them. This gives feedback and recognition to the volunteers who responded to your issue.
  • Arie's avatar
    Arie
    Icon for Altostratus rankAltostratus

    Is there a chance that client requests specify a file (e.g. /index.html, /contact.php)? If so, you won't want to add a slash to those.

     

  • Yes good catch. Use this updated iRule

     iRule to add a slash to requests
    when HTTP_REQUEST {
       skip files
      if {[HTTP::path] contains "."} { return }
       already ends in a slash
      if {[HTTP::path] ends_with "/"} { return } 
    
      HTTP::path "[HTTP::path]/"
      HTTP::redirect [HTTP::uri]
    }