Question

I'm using rack-rewrite to add a url rewrite rule that will strip slashes, but I only want it to do it if there isn't a hashbang following the slash.

I tried the following regex, but it rewrites anyway, even if there is a #! after the /:

^/(?!#!.*)(.*)/$

# The rule
r301 %r{^/(?!#!.*)(.*)/$}, '/$1'

As an example, if the url would be tada/, it should rewrite to tada, but if it were tada/#!WHATEVER_STRINGS_HERE it should not enforce the rule. What is my regex missing ?

Was it helpful?

Solution

You may be missing that URL fragments (a # and everything following it) are never submitted to the server at all. When the browser is asked to visit the URL /foo/bar#baz, it will only request /foo/bar from the server and handle the #baz locally inside the retrieved document.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top