Forum Discussion
Anonymous
Feb 01, 2016check for trailing slash in /foo and /foo/bar
Hi,
I have a case where I want to add a trailing slash if someone hit /foo or /foo/bar. The rules below works but I wonder if it can be optimized:
when HTTP_REQUEST {
add trailing sla...
Brad_Parker
Cirrus
Feb 01, 2016This is a simple way of doing what you want:
when HTTP_REQUEST {
if {not ([HTTP::path] ends_with "/")}{
HTTP::respond 301 noserver Location "${protocol}:://[HTTP::host][HTTP::path]"
}
}
One thing to mention though your original iRule used HTTP::uri, that will contain query parameters if they exist which would make your redirect look like this, http://some.domain.com/originalUri?parameter1=xyz/.
If you want to account for query parameters you could do this:
when HTTP_REQUEST {
if {not ([HTTP::path] ends_with "/")}{
if {[HTTP::query] ne ""}{
set myuri "[HTTP::path]/?[HTTP::query]"
}
else {
set myuri "[HTTP::path]/"
}
HTTP::respond 301 noserver Location "${protocol}:://[HTTP::host]$myuri"
}
}
Brad_Parker
Cirrus
Feb 01, 2016As Kai mentions below, you will need additional logic to account for files that wouldn't want trailing slashes.
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
DevCentral Quicklinks
* 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
Discover DevCentral Connects