[Solved]-Django: what's the difference between a temporary redirect and a permanent redirect?

9👍

There are two ways to return a 301 permanent redirect:

from django.shortcuts import redirect

def my_view(request):
    # some code here
    return redirect('/some/url/', permanent=True)

https://docs.djangoproject.com/en/1.5/topics/http/shortcuts/#redirect

or:

from django.http import HttpResponsePermanentRedirect

    def my_view(request):
        # some code here
        return HttpResponsePermanentRedirect('/some/url')

https://docs.djangoproject.com/en/1.5/ref/request-response/#django.http.HttpResponsePermanentRedirect

12👍

Just adding a note to Brandon’s post concerning your question what is the difference between both. The major difference between temporary and permanent redirects is how third parties see it.
If Google sees a permanent redirect it would probably think that the old site is gone and would update all hits to directly access the new URL. Some people say that Google favors permanent redirects as temporary redirects are often used by spammers.

A case for temporary redirects are e.g. internal rewritings of URLs on your own site that link to a download mirror/static file server. In this case the redirecting site will probably stay the same while the other server address might change (e.g. host static content wherever it is cheapest). Using permanent redirects might introduce problems (think about podcast players on your phone that remember such redirects and won’t recognize it when you move your static file server).

Leave a comment