I had a website with domain www.example1.com
, and recently i have pointed another domain dns www.example2.com
to same server that contains www.example1.com, so both www.example1.com
and www.example2.com
serves the same code but domain name should be different, and I want to get domain names from django request as below
def homepage(request):
domain = request.META['HTTP_HOST']
if domain == 'www.example1.com':
return HTTPResponseReredirect('/new/')
elif domain == 'www.example2.com':
return HTTPResponseReredirect('/old/')
So based on the domain name i want to redirect user to different web pages, but each and every time i used request.META['HTTP_HOST']
the output was an IP address 127.0.0.1:8001
as below
META:{'HTTP_ACCEPT': ‘text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8’,
'HTTP_ACCEPT_ENCODING': 'gzip, deflate, sdch, br',
'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8',
'HTTP_CONNECTION': 'close',
'HTTP_COOKIE': 'sessionid=xxxxxxxxxxxxxxxx; ',
'HTTP_HOST': '127.0.0.1:8001',
'HTTP_UPGRADE_INSECURE_REQUESTS': '1',
'HTTP_USER_AGENT': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
'PATH_INFO': u'/',
'QUERY_STRING': '',
'RAW_URI': '/',
'REMOTE_ADDR': '127.0.0.1',
'REMOTE_PORT': '41280',
'REQUEST_METHOD': 'GET',
'SCRIPT_NAME': u'',
'SERVER_NAME': '127.0.0.1',
'SERVER_PORT': '8001',
'SERVER_PROTOCOL': 'HTTP/1.0',
'SERVER_SOFTWARE': 'gunicorn/19.0.0',
But how to get the actual domain name ?
request.META['HTTP'] shows current domain name on server, or IP address if domain name do not tied.
For example i have this function on my project
def test(request):
return HttpResponse("{}".format(request.META['HTTP_HOST']))
What show`s me test function on local server
What show`s me test function on remote server with domain name aut0parts.site
I assume you testing your code in local server, that`s why your output is 127.0.0.1:8001. If you set domain name to your server I think everything will work :)