#
) or hashbang (#!
) routes. I strongly
recommend using the hashbang approach since that is what Google looks for when
indexing a single-page application. While I am focusing on AngularJS this will
also work with Ember.js, Backbone.js and any other single-page application
framework using a similar navigation style.Basic Setup
This is just a down and dirty way to create a static site with S3. If you need more information on what’s going on, then search for “hosting static site with AWS S3” in your favorite search engine.- Goto the Amazon Web Services Management Console.
- Open the S3 control panel.
- Create a new bucket with your applications domain name as the name, like
app.com
. - Now go ahead and upload your application files to the bucket. Make sure when you upload you select Make everything public on the permission step.
- Open the property panel for the bucket.
- Click Permission and then click Add more permission.
- Select Everyone as the Grantee and check List. This will allow a 404 to return.
- Save your changes.
- Click Static Website Hosting and then Enable website hosting.
- You can fill out
index.html
as the Index Document. - Save your changes
- Copy the address listed under Endpoint in the Static Website Hosting panel.
- Now goto your application’s domain DNS control panel and add a new CNAME record pointing to the endpoint you got from AWS. It is important that the CNAME domain alias matches what you named your bucket.
- That’s it your site should now be working.
Handling Hashbangs
While now you can use your application, but all we really did was just set up a static site. We can go one step further. AngularJS application support routes to control what is displayed to the user. While your site already supports routes as is, it will fail if the user omits the hash or hashbang. For exampleapp.com/#/item/1
works but app.com/item/1
fails. We can fix this using S3 by
having it automatically redirect app.com/item/1
to app.com/#/item/1
.Go back to your application’s bucket properties in AWS. Under Static Website Hosting select Edit Redirection Rules. You should now see an empty text box. Use the code below, make sure to replace the HostName with your applications domain. If you want to use the hashbang method for your URLs simply change
#/
to #!/
.<RoutingRules>
<RoutingRule>
<Condition>
<HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals >
</Condition>
<Redirect>
<HostName>[[ your application's domain name ]]</HostName>
<ReplaceKeyPrefixWith>#/</ReplaceKeyPrefixWith>
</Redirect>
</RoutingRule>
</RoutingRules>
Summary
Congratulations your AngularJS application is now being hosted by Amazon S3. A few things of note before you go. If you want to use CloudFront so your site is hosted via a CDN, the redirection rules won’t be applied by CloudFront if you link directly to the S3 bucket. You’ll have to point CloudFront to the bucket’s end point for it to work properly. I’ll be making a post to discuss setting this up at a later date. Another thing you may consider doing is redirectwww
to
your applications domain in the DNS. Most DNS services have an easy option to
make this happen but you’ll have to check your DNS service documentation. This
can also work in reverse just make sure your bucket name is www.app.com
.