maykinmedia/django-regex-redirects

End of line $ in regex causes $1 query string pass to fail

Opened this issue · 1 comments

There could be a problem between the keyboard and chair and if this is not a bug I'd love to know what I might be doing wrong. At the least perhaps I could help update the docs.

I would like to catch a URL and include the optional query string and match all of these:

/1/2/
/1/2/?x=4
/1/2/?x=4&y=2

To do this I've got this regex:

/1/2/(\?.*)?$

/1/2/ = base
( = wrapping parens to get $1
\? = literal question mark
.* = zero or more anything except line breaks
)
? = 0 or 1 of what's in the parens
$ = and butt up against the end of the string

If that makes sense, and manual testing shows that these redirect nicely:

/1/2
/1/2?x=4
/1/2/?x=4
/1/2/?x=4&y=2
/1/2/

and these don't, as they shouldn't:

/1/2/#anchor
/1/2/3/
/1/2/3/4

Remove the $ and it expands to match everything above which is undesirable.

image

When I add $1 to my redirect URL to catch and forward the query string...

image

/1/2/?x=1 works just fine. But /1/2/ causes an unmatched group error.

Is it possible middleware.py > RedirectFallbackMiddleware.process_response which contains

new_path = redirect.new_path.replace('$', '\\')

might be failing on this?

Thanks!

--

Django==1.9.11
Python==2.7.9
django-regex-redirects==0.0.5

Sorry for not seeing this issue sooner. It seems to me that it might be due to the optional group, and that it can't find the first group in the re.sub() because it wasn't provided in this case.

The tests I think show a workaround: add two redirects, one with and one without the group.

If you submit a new test with this failing case I wouldn't mind giving it a try to solve it.