bmiljevic
bmiljevic

Reputation: 782

Catching URLs with GET parameters

I have and old Wordpress website. It had URLs like:

/?wpsc-product=abc

I have built a new website in Django, with URLs like:

/products/abc/

How can I catch old URLs and redirect them to new ones? Actually, I'm not sure how can I catch URL with parameters.

I'd like to be able to write every single redirection, instead of making regular expression, since I didn't keep all the slugs, some of them are changed.

Upvotes: 0

Views: 198

Answers (1)

Alasdair
Alasdair

Reputation: 308769

You can't catch GET parameters like ?wpsc-product=abc in your urls.py, but you can write a view that checks for GET parameters and redirects to the new view.

Here is a simplified urls.py with two views. The index view redirects requests with parameters and redirects them (e.g. /?wpsc-product=abc), and also handles requests without any parameters (i.e. /, and returns the rendered index template.

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^products/(?P<slug>[-\w]+)/$', views.view_product, name='view_product'),
]

from django.shortcuts import redirect, render

def index(request):
    # check for wpsc-product in GET params
    if 'wpsc-product' in request.GET:
        return redirect('view_product', slug=request.GET['wpsc-product'])
    else:
        # return the regular index template
        return render(request, 'index.html', {})

Upvotes: 2

Related Questions