Search code examples
djangohttp-redirectnginxhttp-status-code-301

Redirecting old PHP links to Django urls


I am getting a lot of search engine referral links for my previous PHP developed site that has now been migrated over to Django. I made a url redirect for the old php links like search.php?name=john+smith to the same view for my django search url as shown here:

urls.py

url(r'^search.php/$', profile_search, name='search'),
url(r'^search/$', profile_search, name='search'),

Will Google eventually update those old links if I redirect through urls.py or do I need to make a 301 redirect? If so how would I do this with django and nginx?


Solution

  • I would do this at nginx level - this is much more efficient than having Django handle it. Assuming the Django view expects the same query arguments, you can do this in your nginx server block:

    location = /search.php { 
        return 301 http://$server_name/search/$is_args$args; 
    }
    

    This will redirect all requests for search.php to /search/, preserving any query arguments.

    A 301 response is definitely the correct approach - you don't want to serve duplicate content on different URLs.