There are few problems with the S3/Redirect based approach mentioned by others.
- MutlipleMultiple redirects happen as your app's paths are resolved. For example: www.myapp.com/path/for/test
www.example.com/path/for/testgets redirected as www.myapp.com/#/path/for/testwww.example.com/#/path/for/test - There is a flicker in the urlURL bar as the '#'
#comes and goes due the action of your SPA framework. - The seoSEO is impacted because - 'Hey! Its googleGoogle forcing his hand on redirects'
- Safari support for your app goes for a toss.
The solution is:
- Make sure you have the index route configured for your website. Mostly it is index.html
index.html - Remove routing rules from S3 configurations
- Put a CloudfrontCloudFront in front of your S3 bucket.
- Configure error page rules for your CloudfrontCloudFront instance. In the error rules specify:
- HttpHTTP error code: 404 (and 403 or other errors as per need)
- Error Caching Minimum TTL (seconds) : 0
- Customize response: Yes
- Response Page Path : /index.html
/index.html - HTTP Response Code: 200
- For SEO needs + making sure your index.html
index.htmldoes not cache, do the following:
- Configure an EC2 instance and setup an nginxNginx server.
- Assign a public ipIP to your EC2 instance.
- Create an ELB that has the EC2 instance you created as an instance
- You should be able to assign the ELB to your DNS.
- Now, configure your nginxNginx server to do the following things: Proxy_pass all requests to your CDN (for index.html
index.htmlonly, serve other assets directly from your cloudfrontCloudFront) and for search bots, redirect traffic as stipulated by services like Prerender.io
I can help in more details with respect to nginxNginx setup, just leave a note. Have learnt it the hard way.
Once the cloud front distribution update. Invalidate your cloudfrontCloudFront cache once to be in the pristine mode. Hit the urlURL in the browser and all should be good.