AppEngine: Redirect naked or non-www domain to www domain on App Engine using Google Apps


Problem Statement:

Your appspot domain: example.appspot.com
Your custom domain: www.example.com
non-www/ naked domain: example.com

How to redirect naked/ non-www version of your custom domain to the www version of the custom domain name?

Generic Method:
Add a 301 Redirect using your DNS provider. For example, if you own a domain at Godaddy, use Godaddy's DNS manager to redirect the non-www version to the www version. Alternatively, Namecheap offers a Free DNS feature for domain names registered with any registrar, which you can use by pointing your DNS servers to Namecheap's free nameservers.

After that, point @ to your www.example.com, and select URL Redirect(301)

Step By Step:

1. Go to Namecheap.com. Select Free DNS from the Products menu.

You will be asked for the domain name, sign up and point your dns servers as instructed.

2. After you have Free DNS up and running, go to Your FreeDNS under the Products menu and select your domain name


In an empty field type and select as follows.
@ www.example.com URL Redirect(301) 
In some time, your naked/non-www domain(i.e http://example.com) will start to redirect to www.example.com.

The following "Google Apps" method is not necessary if you can follow the above method.
Use the Google Apps Method only if:
a. Your DNS service provider does not give you an option to do a 301 Redirect.
b. You do not want to setup a .htaccess/ php/ script based redirect
c. You do not want to change your DNS service provider.


Google Apps Method:
With the Google Apps method, you can redirect your non-www domain to the www domain by pointing your non-www domain to some Google servers (using A records), which then do the necessary redirection 

Pre-requisites:

1. Sign up for Google Apps using the domain in question.
Google offers a free edition of Google Apps which you can use to accomplish this.


2. Add the domain to your App Engine as a custom domain
If you have you app working on www.example.com instead of example.appspot.com, you have already done this.

If you have not, then sign in to your app engine account > select your app > Application Settings > Add Domain


 Add your custom domain www.example.com,


You will be asked to sign in to your Google Apps account, where you can accept access to the app.

Click on Add New URL

 Type in "www" without quotes in the field provided and click "Add"
 The next page will give a step by step procedure of creating a CNAME which points "www" to "ghs.googlehosted.com". Click "I've completed these steps" after you are done.


Going back to our Namecheap example, the DNS settings should look like this

On completion, you will be greeted by the following



3. Now Log in to your Google Apps account at google.com/a > select Sign in> Enter your domain name "example.com" & Select Domain Management from the drop down menu and click Go.


Go to Domain Settings > Domain Names and Create a redirect



In the following screen , enter "www" in the field provided and click "Continue"

The next page will give you the directions to create the required redirect.

Go to your DNS provider and create four(4) A records pointing your naked domain (i.e example.com) to the following Google servers
216.239.32.21
216.239.34.21
216.239.36.21
216.239.38.21


In some time, depending upon your DNS service provider, your redirect will start working.

How to check whether a 301 redirect is working?

A 301 redirect is important for your domain name's serach rankings. So how do you check whether the redirect is working as intended.
First check whether http://example.com goes to www.example.com.

Then, using Firefox, Install the Live HTTP Headers add on, After the add on is installed and firefox has been restarted, go to Tools>Live HTTP Headers, then in the main firefox window type in "http://example.com" in the address bar(without quotes).

The Live HTTP Headers window will show the headers for the session, which will be as below

http://example.com/

GET / HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:15.0) Gecko/20100101 Firefox/15.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

HTTP/1.1 301 Moved Permanently
Location: http://www.example.com/
Date: Tue, 25 Sep 2012 11:43:22 GMT
Content-Type: text/html; charset=UTF-8
Server: ghs
Content-Length: 223
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
----------------------------------------------------------
http://www.example.com/

GET / HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:15.0) Gecko/20100101 Firefox/15.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

HTTP/1.1 200 OK

In the above log, one can see how the browser first requests for http://example.com and gets a reply that says
 
HTTP/1.1 301 Moved Permanently
Location: http://www.example.com/

The browser now makes a GET request for http://www.example.com and is greeted by a 200 OK status!
 
HTTP/1.1 200 OK


Digressing a little bit, also note the server name,
 
Server: ghs

If you had used the first method(Namecheap FreeDNS + URL Redirect 301), the server name would have been different. I tried it out and got the result as

Server: nginx


Nginx is an open source HTTP Server. I do not know what ghs stands for, but it could mean google hosted server. The server name can be changed, so it is not of much importance.

tl;dr
If you want to redirect non-www domain to www, i.e example.com to example.com, use a 301 URL Redirect.
If your DNS does not have that option, switch to one that does(like Namecheap)
If you do not wish to switch DNS, use Google Apps "Change Redirect " option under Domain Settings>Domain Name

Post a comment if you have doubts or spot mistakes in the post.

Comments