You have probably noticed that different websites handle the www subdomain in different ways. For Example:
- google.com redirects you to www.google.com
- www.ipython.org redirects you to ipython.org
- writersreply.com and www.writersreply.com look identical without redirecting
It’s worth understanding DNS, A Records, CNAME, the HTTP Host Request Header, and HTTP Redirects so you can make your domain function the way that you want.
DNS - Domain Name System
When you buy a domain, your registrar allows you to specify Authoritative Name Servers. For example you might register ns1.domaincontrol.com
as the authoritative resource on the internet for identifying the IP address of your domain.
A Records and CNAME
Once your Authoritative Name Server is registered, you configure that name server, specifying the IP addresses that your domain and (optionally) subdomains point to.
An A Record (Address Record) points your domain or subdomain to an IP Address. An A Record will always point to an IP Address.
A CNAME (Canonical Name aka Alias) points your domain or subdomain to to the the IP Address of a different domain. CNAME records always point to domain names.
The **@** symbols is used in DNS records to indicate your root domain name. Consider the hypothetical DNS table
below for the domain writersreply.com
Host Name | Address | Type |
@ | 18.85.28.11 | A |
www | writersreply.com | CNAME |
The example above doesn’t actually redirect users - it just means that when you navigate your browser to www.example.com
the HTTP Request
goes to the same IP address as if you navigate to example.com
. In either case the address bar in your browser will be unchanged when the site loads.
HTTP Redirects
You can’t redirect users with DNS Configuration. However, some DNS providers offer a redirection service. If your Domain registrar does not offer redirection service, you have to configure your web server to redirect http traffic manually.
How does this work? Here’s an example of browser behavior when you navigate to google.com
:
- Use the DNS IP address provided by your Router or ISP to determine the Authoritative Name Server for
google.com
- Answer:
ns1.google.com
- Request the ip address of
google.com
fromns1.google.com
- Answer:
74.125.21.100
- Send an HTTP Request to
74.125.21.100
looking something like this:
GET / HTTP/1.1
Host: google.com
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
DNT: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
X-Chrome-UMA-Enabled: 1
Notice that the HTTP request is sent to an IP address, but it includes the Host Header google.com
. Google’s servers see
the host name in the request, and send back an HTTP 301 Redirect looking akin to this:
HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Server: gws
Content-Length: 219
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Alternate-Protocol: 80:quic
Age: 25150
Date: Sun, 01 Jun 2014 13:09:35 GMT
Expires: Tue, 01 Jul 2014 13:09:35 GMT
Cache-Control: public, max-age=2592000
Connection: keep-alive
Important points
- The only thing that DNS acutally does is tie Domain names to IP Addresses
- HTTP Requests are sent to IP addresses, but include the domain name in the Host Header
- HTTP servers may serve different content depending on the contents of the Host Header
The exact way you configure your server to redirect http requests depends on the server you are using.
- Apache servers can redirect via the .htaccess file (Stackoverflow)
- Nginx servers can redirect via server block (Stackoverflow)
- Node.js servers inspect the HTTP Host header, and headers modify the response object
// Inside node.js request handler
.writeHead(302, {
response'Location': 'your/path/here',
// optional other headers...
;
}).end(); response