Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Implement 301 Redirects with Django/Heroku

I'm looking to redirect a list of old URLs to a list of new URLs in a Django/Heroku app.

Since I'm using Heroku, I can't just use an .htaccess file.

I see that rails has rack-rewrite, but I haven't seen anything like that for Django.

like image 767
dpford Avatar asked Aug 27 '13 15:08

dpford


2 Answers

Django has redirects app, which allows to store redirects list in database: https://docs.djangoproject.com/en/dev/ref/contrib/redirects/

Also here a generic RedirectView:

https://docs.djangoproject.com/en/1.3/ref/class-based-views/#redirectview

And the lowest level is HttpResponseRedirect:

https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpResponseRedirect

like image 92
kubus Avatar answered Sep 20 '22 17:09

kubus


You can use redirect. Please check below code.

from django.shortcuts import redirect
return redirect(
                '/', permanent=True
            )

It worked for me.

enter image description here

like image 40
Wagh Avatar answered Sep 17 '22 17:09

Wagh