Forum Discussion
Want to remove "/" at the end of uri
Hi All,
We wanted to create an irule which should remove "/" from the uri.
Something like if user tries /">http://abc.com/xxxx/ it should be redirected to http://abc.com/xxxx .
I tried following irule but its not working.
when HTTP_REQUEST {
if { [string tolower [HTTP::uri]] ends_with "/" } {
log local0. "Incoming request = [HTTP::host][HTTP::uri]"
set uri [string map -nocase {"/" ""} [HTTP::uri]]
log local0. "New URI = $uri"
HTTP::uri $uri
}
}
Thanks -Rajesh
7 Replies
- shaggy
Nimbostratus
I think "string trimright" is what you need instead of "string map"
when HTTP_REQUEST { if { [string tolower [HTTP::uri]] ends_with "/" } { HTTP::redirect http://[HTTP::host][string trimright [HTTP::uri] "/"] } }
note - this currently also matches a lone "/", so http://abc.com/ will be redirected to http://abc.com
Do you want to redirect the user to the new URI or pass the modified URI to the pool member?
- Rajesh_74831
Nimbostratus
worked like a charm ..... thanx a ton.
For your question of the URI modification of the actual url also works for me ...
Thanks
-Rajesh
- Rajesh_74831
Nimbostratus
I was a bit quick to reply back ... looks like it worked fine for when the URI is present but when accessing the actual url its going in a redirect loop ...
Any suggestions.
- shaggy
Nimbostratus
can you elaborate? does the actual URL redirect to another URL with a trailing "/"? Can you provide an example of the redirect loop (curl results could be handy)? I had no issues with the iRule itself - it sends a redirect in response to any requested URI that has a trailing / (below). If you just want the iRule to modify the URI submitted to the pool member instead of a redirect back to the user, you can use HTTP::uri [string trimright [HTTP::uri] "/"] instead of HTTP::redirect http://[HTTP::host][string trimright [HTTP::uri] "/"] > GET /test/ HTTP/1.1 > User-Agent: curl/7.37.1 > Host: 1.1.1.1 > Accept: */* > * HTTP 1.0, assume close after body < HTTP/1.0 302 Found < Location: http://1.1.1.1/test < Server: BigIP * HTTP/1.0 connection set to keep alive! < Connection: Keep-Alive < Content-Length: 0 < * Connection 0 to host 1.1.1.1 left intact * Issue another request to this URL: 'http://1.1.1.1/test' * Found bundle for host 1.1.1.1: * Re-using existing connection! (0) with host 1.1.1.1 * Connected to 1.1.1.1 (127.0.0.1) port 80 (0) > GET /test HTTP/1.0 > User-Agent: curl/7.37.1 > Host: 1.1.1.1 > Accept: */* > < HTTP/1.1 503 Service Temporarily Unavailable
- Rajesh_74831
Nimbostratus
$ curl -v qa2.opensky.com * Rebuilt URL to: qa2.opensky.com/ * About to connect() to qa2.opensky.com port 80 (0) * Trying 192.168.71.154... * Adding handle: conn: 0x7fc87b80f800 * Adding handle: send: 0 * Adding handle: recv: 0 * Curl_addHandleToPipeline: length: 1 * - Conn 0 (0x7fc87b80f800) send_pipe: 1, recv_pipe: 0 * Connected to qa2.opensky.com (192.168.71.154) port 80 (0)
GET / HTTP/1.1 User-Agent: curl/7.33.0 Host: qa2.opensky.com Accept: /
- HTTP 1.0, assume close after body < HTTP/1.0 302 Found < Location: https://qa2.opensky.com/ < Server: BigIP
- HTTP/1.0 connection set to keep alive! < Connection: Keep-Alive < Content-Length: 0 <
- Connection 0 to host qa2.opensky.com left intact
- Rajesh_74831
Nimbostratus
bottom line is http://qa2.opensky.com/ should not be redirected or trimmed and any other uri associated with / should be trimmed.
I am new to the application and irules, so bear with my ignorance if any.
Thanks -Rajesh
- shaggy
Nimbostratus
This ought do it - if the URI is more than one character in length and the URI ends with /, the iRule will redirect to the same URI with all right-most /'s stripped. abc.com/test/// will redirect to abc.com/test
when HTTP_REQUEST { if { [string length [HTTP::uri]] > 1 and [string tolower [HTTP::uri]] ends_with "/" } { HTTP::redirect http://[HTTP::host][string trimright [HTTP::uri] "/"] } }
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com