- Newest
- Most votes
- Most comments
In order to run multiple apps under the same domain, you'll need to use Lambda@Edge or Cloudfront Functions to implement the functionality you need.
There are two options I can think of - either you examine the request and rewrite the URI path to /app/index.html, or you examine the response where you'd look for error codes and adjust the status code and body accordingly (see example here)
I think the response option is slightly more complex as you'd either need to embed the contents of index.html inside the function, or make a network call from the function to retrieve it from S3.
For the request option, I don't think we have any sample CloudFront functions that do exactly this, but there is one here that is close - just adjust the IF statements to suit- eg.
if (uri.startsWith('/app1/')) {
request.uri = '/app1/index.html';
}
else if (uri.startsWith('/app2/')) {
request.uri = '/app2/index.html';
}
Thanks to Paul L... Here is the python solution and a few notes about using python..
- You can't use a template, you have to create the lambda manually (make sure you don't select arm, only works with x86)
- Add a trigger for your cloudfront, for me I added it to my /app/* behavior and once I got that working I added to my /app2/* behavior, etc. You need to deploy to both to get them on the same verison.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import json
from os.path import splitext
def lambda_handler(event, context):
request = event['Records'][0]['cf']['request']
#print("Request before ", request)
ext = splitext(request['uri'])[1]
#print("extension = ", ext)
#only do this for URI's with no extension!!!
if (ext == ""):
if request['uri'].startswith('/app'):
request['uri'] = '/app/index.html'
elif request['uri'].startswith('/app2'):
request['uri'] = '/app2/index.html'
#print("Request after ", request)
return request
Hi, have you tested the Path Pattern option [1] ? That will give you a way to use specific origins for specif routes or paths.
not sure how that would help. According to every article we've found, you need to override 403 response to return 200 and the react apps index page. Path pattern seems to be just a way to control caching. How can I use it to solve this problem?
Relevant content
- asked 4 months ago
- AWS OFFICIALUpdated 3 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated a year ago
Of course I tried the hard way because that's what I started with, but you were right, origin request was the best and easier solution