aws-amplify / aws-amplify/amplify-hosting

Using regex matches in redirects

Open
#570 16 comments 3 reactions 0 assignees View on GitHub
feature-request redirects
Dominant language
Dockerfile
Stars
481
Forks
123
PR merge metrics
No merged PRs in 30d

Description

** Please describe which feature you have a question about? **
Rewrites and redirects

** Provide additional details**
I have a SPA that does client side routing. However I've been working on my SEO and want to get good 301s and 404s. I am also pre-rendering my routes to static html files, which to some degree makes the routing not behave as a SPA from a server perspective.

So I want to have:
301 "/something/" -> "/something"
301 "/something/index.html" -> "/something"
200 "/something" -> "/something/index.html"

However I also need to handle some static files that are present in the root directory, like the favicon and robots.txt etc.

I also have a "static" directory with some other assets in it that I want to exclude from the rewrites and behave like normal 200/404s.

So in my config I have:
```
[
{
"source": "/static/<*>",
"target": "/static/<*>",
"status": "200"
},
{
"source": "/",
"target": "/index.html",
"status": "200"
},
{
"source": "/index.html",
"target": "/",
"status": "301"
},
{
"source": "//index.html",
"target": "/",
"status": "301"
},
{
"source": "//",
"target": "/",
"status": "301"
},
{
"source": "",
"target": "<$1>",
"status": "200"
},
{
"source": "/",
"target": "//index.html",
"status": "200"
},
{
"source": "/<*>",
"target": "/404.html",
"status": "404-200"
}
]
```

This ALMOST works, except for the regex rule which is matching the static files in the root. The regex code is correct and is matching to the files I want. However the target location is not correct (results in a 404 response). What I want it to do is much like the rule I have for the "static" directory where it gives an accurate respose for if the file exists or not (200/404). Which means the target of that rule needs to be the regex match.

I have tried using "<*>", "<$1>", and "<1>" with no joy. Can you tell me the correct way to use the match from the regex? Or if not, what would be a better way to configure this?

Contributor guide

Open the contributing guide

Research direction

Start with the AWS Amplify Hosting documentation for rewrites and redirects, then reproduce the posted JSON configuration with a root static file and a file under static. Check how regex captures are supported in source and target fields and whether the desired 200/404 behavior can be verified for existing and missing assets; done means a documented supported configuration or a confirmed limitation and workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, json
Domain
cloud, web-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.