amplify-hosting icon indicating copy to clipboard operation
amplify-hosting copied to clipboard

How to properly redirect using regex

Open akefirad opened this issue 5 years ago • 15 comments

** Please describe which feature you have a question about? ** I'm trying to redirect /<3-digit-integer> to /code/<same-integer> but for some reason I can't get it working. What I tried so far (I wasn't sure about the exact syntax):

/<[0-9]{3}> to /code/<*>
/</[0-9]{3}/> to /code/<*>
/<^[0-9]{3}$> to /code/<*>
/</^[0-9]{3}$/> to /code/<*>

Can someone please take a look and tell me if I'm missing anything here. Thanks.

akefirad avatar Jul 01 '20 08:07 akefirad

Please refer to our documentation which provides examples on redirects. https://docs.aws.amazon.com/amplify/latest/userguide/redirects.html

Let us know if that doesn't work for you.

nimacks avatar Jul 01 '20 23:07 nimacks

No, not really. I already talked to AWS support and showed the whole thing. For some reason the exact regex above doesn't work.

akefirad avatar Jul 02 '20 09:07 akefirad

@akefirad Did you ever get a resolution to this? It seems to me like regular expressions just do not work as advertised in the documentation.

WolfyUK avatar Feb 04 '21 12:02 WolfyUK

Nope. Seems messed up 😞

akefirad avatar Feb 06 '21 10:02 akefirad

Resolving due to age - please re-open if you are still experiencing issues.

ganipcanot avatar Jul 27 '21 04:07 ganipcanot

Resolving due to age

Not sure if "age" can resolve such issues. Perhaps "Closing due to age" is a better wording.

please re-open if you are still experiencing issues.

Not sure why we should retry. There's no update regarding any fix. If nothing is fixed then people would still be experiencing the issue.

akefirad avatar Jul 27 '21 05:07 akefirad

I'm experiencing this exact same problem

simonjpartridge avatar Sep 14 '21 20:09 simonjpartridge

It's impossible to have regex working. For example, I want a 404 rewrite to index for ANY 404 path ending with "download/": /</^.+/download/?$/> -> index.html (404-200 rewrite)

Why this is not working (never match). If I use an exact URL it works perfectly. Are there any limitations on the number of 404 rewrites?

iceye avatar Oct 12 '21 00:10 iceye

I'm seeing the same - regular expressions don't seem to work at all.

kookster avatar Nov 04 '21 15:11 kookster

December 2021: I'm having the issue as well.

amatzen avatar Dec 02 '21 10:12 amatzen

I think you need to escape the / characters like so \/ Can't get capture groups working though. Desperately needed - as per https://github.com/aws-amplify/amplify-hosting/issues/570

taylormadeapps avatar Jun 09 '22 16:06 taylormadeapps

Please refer to our documentation which provides examples on redirects. https://docs.aws.amazon.com/amplify/latest/userguide/redirects.html

Let us know if that doesn't work for you.

The only reference to regex is https://docs.aws.amazon.com/amplify/latest/userguide/redirects.html#redirects-for-single-page-web-apps-spa which does not demonstrate capture groups. The docs also do not specifically mention regex anywhere. There is a large opportunity to better document regex usage.

tatethurston avatar Nov 23 '22 20:11 tatethurston

Agreed the documentation on regex in redirects is basically nonexistent. Can't get anything to work

jackdewhurst avatar Dec 01 '22 11:12 jackdewhurst

Agree, they need better documentation for this.

dannyhchan avatar Feb 01 '23 01:02 dannyhchan

I don't know if a capture group is possible, but I was able to make something work which is close enough for my purpose:

Source: </^\/(?!frontend\/).*$/><*> Target:/frontend/<*>

So it is possible to combine the capture-like thing <*> with a regex. Please note that you have to escape forward slashes as shown in my example. This is probably why it did not work for @akefirad and @iceye. Apparently, regexes need to be placed inside of </ and />.

It is sad that AWS is unable to resolve this and provide proper docs. This issue has been reported almost four years ago!

Janosch avatar Jun 12 '24 15:06 Janosch

@Janosch I'm checking your regex and It is not working for me, also I've found something wierd. When I save the changes all the "" are removed automatically from the regex.

Before save: Screenshot 2024-10-22 at 18 40 57

After save Screenshot 2024-10-22 at 18 41 14

AlejandroPerez92 avatar Oct 22 '24 17:10 AlejandroPerez92