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.

[![](https://i.stack.imgur.com/alc3w.png)](https://i.stack.imgur.com/alc3w.png)

Here is the domain forwarding information I tried:
[![enter image description here](https://i.stack.imgur.com/TeeiC.png)](https://i.stack.imgur.com/TeeiC.png)
['https://webmasters.stackexchange.com/questions/101120', 'https://webmasters.stackexchange.com', 'https://webmasters.stackexchange.com/users/71988/']
Update: There are 3 ways to do this. 

1. htaccess
2. php to yaml with redirect
3. 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.
> 
> 
> 

[Full article on Google](https://cloud.google.com/appengine/docs/python/console/using-custom-domains-and-ssl?hl=en)

[Link to Custom Domains Login on Google](https://console.cloud.google.com/appengine/settings/domains?_ga=1.59431789.139328086.1479124615)

**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](https://cloud.google.com/appengine/docs/php/config/appref), 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](https://www.w3.org/TR/WCAG20-TECHS/H76.html) which is client side solution. Or you can host your naked domain somwehere else(Github Pages, Firebase are free solution) and do 301 redirection.