Search code examples
urlember.jsamazon-s3ember-cli

Deploying ember-cli app to S3 without breaking URLs


I'm working on a ember-cli app that I am deploying in S3. I would really like to be able to use this 'serverless' approach, as it is extremely simple to configure, and extremely affordable.

I have a problem with URLs. If I hit http://my-bucket.s3-website-us-east-1.amazonaws.com/ it works just fine. But if I try to directly load a page other than the root of the application, such as http://my-bucket.s3-website-us-east-1.amazonaws.com/elephants/5, then it gives a 403, as there is no such resource in S3. (I can navigate to such pages fine through the application, and I can hit them directly in dev mode on my machine, so the ember app is working fine.)

Looking for solutions, I find suggestions to add #! to my path. This seems better, as it doesn't return a 403, but when I hit http://my-bucket.s3-website-us-east-1.amazonaws.com/#!/elephants/5, it simply redirects to http://my-bucket.s3-website-us-east-1.amazonaws.com, losing any specific information that the path contained.

What are my options? Is there a way to use S3 and have working URLs? Do I need a server? Or is there another approach that has eluded me?


Solution

  • OK, I was close. Turns out that the right approach was:

    http://my-bucket.s3-website-us-east-1.amazonaws.com/#/elephants/5

    I had tried various approaches, but none of them had been precisely a # with no !, and surrounded by /. Relevant reading is the documentation on location

    It is worth noting that I am using:

    App.Router.reopen({
      location: 'auto'
    });
    

    Also, I'm using the following RoutingRules under Static Website Hosting

    <RoutingRules>
        <RoutingRule>
            <Condition>
                <HttpErrorCodeReturnedEquals>403</HttpErrorCodeReturnedEquals>
            </Condition>
            <Redirect>
                <HostName>my-bucket.s3-website-us-east-1.amazonaws.com</HostName>
                <ReplaceKeyPrefixWith>#/</ReplaceKeyPrefixWith>
            </Redirect>
        </RoutingRule>
    </RoutingRules>