[Answer]-How to redirect .aspx pages to new pages on django site in SEO-friendly way

1đź‘Ť

That is not your problem. Your problem is that query parameters (?ID=123) are not part of the URL. You should just match against /detail.aspx/ and get the parameters in the view with request.GET['ID'].

Actually, you shouldn’t do that at all. This level of redirection is much better handled by your web server configuration, eg with mod_rewrite in Apache. There’s no need to invoke the overhead of a Django view to do this sort of thing.

0đź‘Ť

I got this to work, but I’m still not completely happy with it b/c it’s a “2-step hop” for the SEO bots. Here’s what I’ve got:

In Apache conf:

RedirectMatch 301 ^/detail\.aspx(.*) /article$1

This sends the querystring as a parameter to a view:

def mygreatview(request):
    ID = request.GET['ID']
    article = get_object_or_404(Article, url_id=ID)
    url = '/articles/' + article.URL
    return HttpResponsePermanentRedirect(url)

Wish I could do it in 1 hop, but I’ve got to use the ID to lookup the slug. Don’t see how I can get around that one, but should be fine.

👤Rob L

0đź‘Ť

Why not the following? (haven’t tested)

RewriteCond %{QUERY_STRING} ^ID=(\w+)$
RewriteRule ^/detail.aspx /articles/%1?

see http://wiki.apache.org/httpd/RewriteQueryString for more examples.

This will take your old URLs and redirect them to your django view like it expects without having to put any hacky code in your django view.

👤Ken Cochrane

Leave a comment