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!
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.
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)
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!
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.
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.
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
.
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