Forum Discussion

Damion_19248's avatar
Damion_19248
Icon for Nimbostratus rankNimbostratus
Feb 02, 2013

Remove trailing slash

I need to remove any trailing slashes off any url that come in.

 

www.mydomain.com/category/ - -> www.mydomain.com/category

 

www.mydomain.com/place/ --> www.mydomain.com/place

 

etc

 

I found two examples after doing a search but was not able to follow the logic to mold it to what I needed. I am hoping someone is willing to help me.

 

Thanks for your help.

 

7 Replies

  • Something like the following should work:

    
    when HTTP_REQUEST {
    if { [HTTP::uri] ends_with "/" } {
    HTTP::uri [string trimright [HTTP::uri] "/"]
    }
    }
    
  • Sorry, the above would actually break the app if the URI was simply "/". Try this instead:

    
    when HTTP_REQUEST {
    if { not ( [HTTP::uri] equals "/" ) and ( [HTTP::uri] ends_with "/" ) } {
    HTTP::uri [string trimright [HTTP::uri] "/"]
    }
    }
    

    The browser automatically adds the trailing slash if you only enter the host name so the BIG-IP will always see, at a minimum, the single trailing "/".
  • Thanks for you response. I got the following error when I tried to create the rule. Is it compatible with 9.x code or is this 10-11x?

     

     

     

    1:3: Rule [RemoveTrailingSlashes] error:

     

    line 1: [wrong args] [when HTTP_REQUEST ]

     

    line 2: [undefined procedure:

     

    if { not ( [HTTP::uri] equals "/" ) and ( [HTTP::uri] ends_with "/" ) }

     

    {

     

    HTTP::uri [string trimright [HTTP::uri] "/"]

     

    }

     

    ] [{

     

    if { not ( [HTTP::uri] equals "/" ) and ( [HTTP::uri] ends_with "/" ) }

     

    {

     

    HTTP::uri [string trimright [HTTP::uri] "/"]

     

    }

     

    }]

     

  • There shouldn't be anything in the code that's specific to v10+. Can you isolate which command it's saying is undefined?
  • I take that back I tried to format it a little by adding additional whitespace and that seems to have broken it. I placed the command just as you typed it and it accepted it. I will test it and let you know the rules. Thanks again for your help.
  • Say I need abc.def.com/emp should redirect to https://abc.def.com/redirect/redirect.aspx?src=emp.

     

    Is the below iRule correct ?

     

    when HTTP_REQUEST { HTTP::respond 301 Location "https://abc.def.com/redirect/redirect.aspx?src=[HTTP::uri]" }

     

    How can I remove the "/" before [HTTP::uri] ? I need ?src=emp and not ?src=/emp