Learn F5 Technologies, Get Answers & Share Community Solutions Join DevCentral

Filter by:
  • Solution
  • Technology
Answers

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.

0
Rate this Question

Answers to this Question

placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

I forgot to mention that I am on 9.4.5.

0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
Something like the following should work:


when HTTP_REQUEST {
	if { [HTTP::uri] ends_with "/" } {
		HTTP::uri [string trimright [HTTP::uri] "/"]
	}
}
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
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 "/".
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
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] "/"]
}
}]
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
There shouldn't be anything in the code that's specific to v10+. Can you isolate which command it's saying is undefined?
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
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.
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

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

0