JEEND0
JEEND0

Reputation: 482

Django URLConf Redirect with odd characters

I'm getting ready to move an old Classic ASP site to a new Django system. As part of the move we have to setup some of the old URLs to point to the new ones.

For example, http://www.domainname.com/category.asp?categoryid=105 should 301 to http://www.domainname.com/some-category/

Perhaps I'm regex stupid or something, but for this example, I've included in my URLconf this: (r'^asp/category\.asp\?categoryid=105$', redirect_to, {'url': '/some-category/'}),

My thinking is that I have to escape the . and the ? but for some reason when I go to test this, it does not redirect to /some-category/, it just 404s the URL as entered.

Am I doing it wrong? Is there a better way?

Upvotes: 0

Views: 373

Answers (2)

mipadi
mipadi

Reputation: 410542

To elaborate on Daniel Roseman's answer, the query string is not part of the URL, so you'll probably want to write a view function that will grab the category from the query string and redirect appropriately. You can have a URL like:

(r'^category\.asp', category_redirect),

And a view function like:

def category_redirect(request):
    if 'categoryid' not in request.GET:
        raise Http404
    cat_id = request.GET['category']
    try:
        cat = Category.objects.get(old_id=cat_id)
    except Category.DoesNotExist:
        raise Http404
    else:
        return HttpResponsePermanentRedirect('/%s/' % cat.slug)

(Altered to your own tastes and needs, of course.)

Upvotes: 2

Daniel Roseman
Daniel Roseman

Reputation: 599450

Everything after the ? is not part of the URL. It's part of the GET parameters.

Upvotes: 0

Related Questions