Amplify-console: Regex Syntax in Redirect/Rewrite Feature Not Clear

Created on 19 Jun 2020  路  5Comments  路  Source: aws-amplify/amplify-console

* Please describe which feature you have a question about? *
Trying to figure out how to use regex within the redirect/rewrite feature

* Provide additional details*

I wanted "darsx.org" to redirect to "darsx.org/x9" with a HTTP 301
however that ends up looping into "darsx.org/x9/x9/x9/x9".

And so I tried to add a regex match for "darsx.org" without any path trailing, to redirect, so that when it goes to darsx.org/x9 it doesnt redirect anymore.

https://regex101.com/r/TfIA7F/1

I tried putting it in a similar format I've seen other examples:

But it doesn't seem to be working! Any insight into the regex matching syntax and what could be going on, would be helpful! Thanks :)

(currently if you try the URL it's working, it's been solved elsehow, but I'd like to investigate this regex matching feature still)

Most helpful comment

@svjacob Unfortunately, at the current moment we don't have a redirect feature per sub domain. I have added a similar issue #773 as a feature request yesterday. Also, the regex currently works only with sub-directories(path), and not the sub domain. We will let you know once its out :)

All 5 comments

@svjacob The reason for the infinite redirects is the following:

Say you have the following 301 rule:

https://foo.com -> https://foo.com/bar

Now you request the url as https://foo.com. The following happens in sequence:

  1. We extract the host name from the url (https://foo.com) to a given rule鈥檚 source.
  2. If they match, we then see if its a 301 (in this case, there is a match with the above rule, and its a 301)
  3. If so, we apply the target url and append the given subdirectory path as the location header (in this case there is no subdirectory, so the resulting url will be: https://foo.com/bar)

Now, the browser gets a 301 response, and requests the new url (https://foo.com/bar). The host is still https://foo.com. So it matches with the above rule, and appends the /bar to the target = https://foo.com.bar/bar, and so on

The reason for having this logic is for subdomain redirects. Take for example the following rule:

301 redirect rule: https://foo.com -> https://www.foo.com

This would automatically redirect any urls starting with https://foo.com to https://www.foo.com (e.g. https://foo.com/bar -> https://www.foo.com/bar)

In order for your use case to work, you can add the following rule using /:

301 redirect rule: / -> /x9

Let me know if this works.

In order for your use case to work, you can add the following rule using /:

301 redirect rule: / -> /x9

@abhi7cr thanks for the detailed response, yeah I figured that was the reason for the infinite redirects, and unfortunately this / to /x9 redirect rule won't work, because I have two domains added to the amplify app, and I only want one of those domains to redirect to x9.

This is why I am trying to use the regex syntax, because I was hoping it wasn't a domain level match and more so a match by regex.

Any idea if there's a solution for a situation like this? Two domains, same app, one domain / redirect to /x9 --?

@svjacob Unfortunately, at the current moment we don't have a redirect feature per sub domain. I have added a similar issue #773 as a feature request yesterday. Also, the regex currently works only with sub-directories(path), and not the sub domain. We will let you know once its out :)

@abhi7cr thank you for the prompt responses. appreciate it.

Closing this ticket. Please let us know if you have further questions.

Was this page helpful?
0 / 5 - 0 ratings