-1

I try to code to django a redirect fonction. I provide the url, and i want to redirect to the provided URL.

in my urls.py:

urlpatterns = patterns('',
 url(r'^redirect/(?P<name>.*)$', redirect),
 # ...
)

when i test the function with a standard link (ex: google.com), it works perfectly.

when i test the function with a link that containt a "?" character, only the part before the "?" is taken into account.

Example :

"GET /redirect/http://www.polyvore.com/lords_liverpool_hey_jude_tee/thing?id=53713291 HTTP/1.1" 302 0

name = http://www.polyvore.com/lords_liverpool_hey_jude_tee/thing

the ?id=53713291 is not taken into account....

i though that .* means all kind character, wrong?

Do you know what happens ? and how to take the entiere url what ever the character it contains?

thank you very much for your help.

Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97

2 Answers2

0

You seems to don't understand how URL works. Everything after the ? is parsed as arguments for your current view. If you print data in your request.GET dict, you'll find something like:

 {'id': 53713291}

The only way to fix that is to urlencode your URL in the argument and decode it before the redirection.

 >>> import urllib
 >>> urllib.quote_plus("http://www.polyvore.com/lords_liverpool_hey_jude_tee/thig?id=53713291")
 'http%3A%2F%2Fwww.polyvore.com%2Flords_liverpool_hey_jude_tee%2Fthing%3Fid%3D5313291'
 # You should make your URL with this value, for example:
 # /redirect/http%3A%2F%2Fwww.polyvore.com%2Flords_liverpool_hey_jude_tee%2Fthing%3Fid%3D5313291
 # And in your view, use unquote_plus before the redirection:
 >>> urllib.unquote_plus('http%3A%2F%2Fwww.polyvore.com%2Flords_liverpool_hey_jude_tee%2Fthing%3Fid%3D5313291')
 'http://www.polyvore.com/lords_liverpool_hey_jude_tee/thing?id=5313291'

More information about Query String on Wikipedia.

Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
  • I don't think this is going to help him. He needs to start by learning about regex. – jpwagner Dec 01 '13 at 15:18
  • The problem isn't about the regex. It's about how URL works with the HTTP standard. The `?` character has a special meaning, don't forget it. Everything after him is taken as GET parameters. – Maxime Lorant Dec 01 '13 at 15:19
  • In a regex there's a way to escape it, that's not a problem. But in an URL, `redirect/http://domain.tld?id=blabla`, the id=blabla is a query string for the address `redirect/http://domain.tld`, the only way of avoid that (without dealing with ugly random GET dict in the redirect view...) is to get rid of special characters by encoding the URL... The URL pattern is fine here, `.*` will take everythin after the `redirect/` string. – Maxime Lorant Dec 01 '13 at 15:24
0

You're passing in a regex, so you need to escape the special characters (ie, \?)

But if you're trying to pass in querystring parameters you'll want to handle that differently: https://stackoverflow.com/a/3711911

Community
  • 1
  • 1
jpwagner
  • 553
  • 2
  • 8