I have a Google App Engine Java web app that I have setup with a custom domain from GoDaddy. The site can currently be reached at domain.com and www.domain.com. I am trying to have a 301 redirect occur whenever someone tries to access domain.com instead of www.domain.com. I have tried to use domain forwarding via GoDaddy, without success. How can I achieve this goal?
Here is my Google App Engine setup. I have added all of these records to the GoDaddy DNS Manger.
-
The GoDaddy thing is limited and we get questions on the topic a lot. It is far better to ignore the option and work within the web server.– closetnocNov 15, 2016 at 6:10
2 Answers
Update: There are 3 ways to do this.
- htaccess
- php to yaml with redirect
- Within Google itself.
Specify the domain and subdomains you want to map.
Note: The naked domain and www subdomain are pre populated in the form. A naked domain, such as example.com, maps to http://example.com. A subdomain, such as www, maps to http://www.example.com. Click Submit mappings to create the desired mapping. In the final step of the Add new custom domain form, note the resource records listed along with their type and canonical name.
Link to Custom Domains Login on Google
HTACCESS
Assuming you are on their Apache server you want to add this to your .htaccess file. This is for both, choose one. Replace example with your domain name.
#Force www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
#Force non-www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
Google does not support htaccess file. They support app.yaml file, which have different mod rewrite rule, but as far I know It will applicable only to one CNAME, it means if you have set www.example.com then those rewrite rules will not applicable to example.com.
You can use wildcard *
in CNAME, which redirect all of your subdomain to your perferred domain name. For example if people access example.com/directory/ then it will automatically redirect to www.example.com/directory/ . The main drawback of wildcard is, it applied to all of your subdomain, it means you can't add any of content like images and blog in your subdomain, for example blog.example.com will redirect to www.example.com and images.example.com/puppy.jpg will redirect to www.example.com/puppy.jpg(Which may trigger 404 error). But If you don't have placed any of thing in your subdomain then wildcard will solve your problem.
And, If you just want to redirect example.com to www.example.com and not other subdirectory then you can use meta refresh tag which is client side solution. Or you can host your naked domain somwehere else(Github Pages, Firebase are free solution) and do 301 redirection.
-
The OP must already have a CNAME record for their site to be accessible by the
www
subdomain (as well as the apex domain). But a CNAME doesn'tredirect
as you suggest.– MrWhiteNov 16, 2016 at 17:41 -
When we put domain name in App engine then it said which domain we want to use, so here OP already choose www.example.com, so if he put wildcard CNAME instead of www CNAME, then App engine will redirect all subdomain traffic to www version because app engine remember our settings option that we have done earlier.– GoylloNov 16, 2016 at 19:17