rack-rewrite
rack-rewrite copied to clipboard
Removing extra slashes in end of URL
Not sure if it's ok to ask you about it here, but I hope you can help me.
I wanted to create redirect from "domain.com//////" to "domain.com/"
Is it possible?
Think it's gonna be helpful for other users if that is possible, so maybe you could include it in documentation.
tracking, how did you solve this? I am also looking for a way to enforce trailing slashes.
This is a very old issue, but in case it helps anyone:
use Rack::Rewrite do
# remove trailing slashes
moved_permanently %r{(/.*)/(\?.*)?$}, '$1$2'
end
It retains query strings after the trailing slash. You could make it /+ to handle domain.com//////
Thank you @granth
@granth Thank you for the solution. Do you know how to covert localhost:3000/en/articles///ads/// to localhost:3000/en/articles/ads ?
@IvanDreamer I haven't used it, but you can pass a proc as an argument and gsub all the slashes there. See https://github.com/jtrupiano/rack-rewrite#arbitrary-rewriting
something like:
moved_permanently %r{(.*)}, ->(match, env) { match.to_s.gsub %r{//+}, "/" }
That matches every path and replaces each set of multiples slashes with a single slash.
@granth Thank you for the response. Unfortunately it gives me "Too many redirects" 👎