6

Normally i would do it with .htaccess but django doesn't have it.

So what is the best way and what is the code for it to redirect from www.olddomain.com to www.newdomain.com?

NOTE: we are not using Apache, but Gunicorn

thanx!

mgPePe
  • 5,677
  • 12
  • 52
  • 85

8 Answers8

6

The best way to do this is still with your web server rather than Django. This will be much quicker and more efficient than doing it with Django.

Check out this question for more info.

UPDATE

If you really want to do it within django then edit your url conf file (which manages django's url dispatcher) to include the following at the top -

from django.views.generic.simple import redirect_to

urlpatterns = patterns('',   
    (r'^.*$', redirect_to, {'url': 'http://www.newdomain.com'}),
)

For more info check out the documentation.

Community
  • 1
  • 1
Aidan Ewen
  • 13,049
  • 8
  • 63
  • 88
  • Unfortunately we are not using apache so i can use .htaccess, but `http://gunicorn.org/` instead – mgPePe Feb 07 '13 at 14:10
  • So you should redirect with gunicorn (I would be very surprised if it didn't provide that functionality). otherwise edit the wsgi script as described in the question I linked to. – Aidan Ewen Feb 07 '13 at 14:22
  • The suggestions in the question I linked to are better. However I've updated my answer to include an easy to implement solution. – Aidan Ewen Feb 07 '13 at 16:05
  • You shouldn't use gunicorn as a frontend webserver, it should be behind a proxy webserver like Apache or Nginx. – Bouke Feb 07 '13 at 16:09
  • 1
    For django 1.9 check [RedirectView](https://docs.djangoproject.com/en/1.9/ref/class-based-views/base/#redirectview) – Eduardo Mar 02 '16 at 21:20
4
import urlparse
from django.http import HttpResponseRedirect

domain = request.GET['domain'] 
destination = reverse('variable_response',args=['Successful'])
full_address = urlparse.urljoin(domain, destination)
return HttpResponseRedirect(full_address)
radtek
  • 34,210
  • 11
  • 144
  • 111
catherine
  • 22,492
  • 12
  • 61
  • 85
1

i had the same problem so i wrote this and it worked perfectly for me, maybe someone else needs it too:

urlpatterns += [  # redirect to media server with same path
url(r'^media/', redirectMedia),
]

and using this function to redirect:

from urllib.request import urlopen
from django.http import HttpResponse
def redirectMedia(request):
    x = urlopen("http://www.newdomain.com" + request.path)
    return HttpResponse(x.read())

enjoy it!

Ebrahim Karimi
  • 732
  • 8
  • 24
1

For Django >= 2.0 , the easier solution is to use RedirectView

For example in urls.py :

from django.views.generic.base import RedirectView

urlpatterns = [
    path('my_ext_uri', RedirectView.as_view(url='https://YOUR_EXTERNAL_URL')),
]

[Side note]

As mentioned in Aidan's answer , it would be better to redirect requests which will be handled by different services at web server gateway, rather than at (Python/Django) application server.

Ham
  • 703
  • 8
  • 17
0

I ended up doing it using heroku and spinning up 1 web dyno (which is free).

#views.py
def redirect(request):
    return render_to_response('redirect.html')

#redirect.html
<html>
<head>
<title>Blah</title>
<meta http-equiv="refresh" content="1;url=http://www.example.com">
</head>
<body>
<p>
Redirecting to our main site. If you're not redirected within a couple of seconds, click here:<br />
<a href="http://www.example.com">example.com</a>
</p>
</body>
</html>

Simple as that. Can find the same example here.

GetItDone
  • 566
  • 7
  • 22
0

An alternative to catherine answer, updated to Python 3 is:

from django.contrib.sites.shortcuts import get_current_site
from urllib.parse import urljoin
from django.http import HttpResponseRedirect

NEW_DOMAIN = 'www.newdomain.com'

Put in each view:

def myView(request, my_id):
    if request.META['HTTP_HOST'] != NEW_DOMAIN:
        # remove the args if not needed
        destination = reverse('url_tag', args=[my_id])
        full_address = urljoin(DOMAIN, str(destination))
        return HttpResponseRedirect(full_address)
    # your view here        

The url_tag is the one defined in urlpatterns.

Community
  • 1
  • 1
msampaio
  • 3,394
  • 6
  • 33
  • 53
0

I ended with simple solution:

return HttpResponse(f"<script>location.replace('https://example.com/');</script>")

It works if user don't disable scripts in webbrowser

Waldemar Podsiadło
  • 1,365
  • 2
  • 5
  • 12
0

You could do it simply adding the redirect in the value 'urlpattens' found in 'urls.py'. Hope it helps. The reference is source.

from django.shortcuts import redirect


urlpatterns = [
    path('old-path/', lambda request: redirect('new-path/', permanent=False)),
]
ricogordo
  • 36
  • 2