LP Square
LP Square

Reputation: 1135

Firebase redirect using Regex

My goal is to redirect any URL that does not start with a specific symbol ("#") to a different website. I am using Firebase Hosting and already tried the Regex function in redirect to achieve this. I followed this firebase documentation on redirects but because I new to regular expressions I assume that my mistake might be my regex code.

My Goal:

My Code:

{
  "hosting": {
    ...
    "redirects": [
      {
        "regex": "/^[^#]:params*",
        "destination": "otherdomain.com/:params",
        "type": 301
      }
    ],
    ...
  }
}

Upvotes: 2

Views: 788

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627537

You can use

"regex": "/(?P<params>[^/#].*)"

The point is that you need a capturing group that will match and capture the part you want to use in the destination. So, in this case

  • / - matches /
  • (?P<params>[^/#].*) - Named capturing group params (you can refer to the group from the destination using :params):
    • [^/#] - any char other than / and #
    • .* - any zero or more chars other than line break chars, as many as possible

To avoid matching files with .js, you can use

/(?P<params>[^/#].*(?:[^.].{2}$|.[^j].$|.{2}[^s]$))$

See this RE2 regex demo

See more about how to negate patterns at Regex: match everything but specific pattern.

Upvotes: 2

Related Questions