Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
julesallen opened this issue Jan 30, 2017 · 1 comment
Open

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

julesallen opened this issue Jan 30, 2017 · 1 comment

Comments

@julesallen
Copy link

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

@alextreme
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants