[Firebase Hosting] Redirect Glob Negation, Capture Rewrites

1,672 views
Skip to first unread message

Charles Aylward

unread,
Feb 7, 2018, 8:30:21 PM2/7/18
to Firebase Google Group
I noticed that while Firebase hosting utilizes glob notation in the firebase.json file for redirects and rewrites, it's missing some pretty significant glob functionality.

1. Redirect Glob Negation

"redirects": [ {
"source" : "/test/one-!(a|b|c)/:end.html",
"destination" : "/test/one/:end.html",
"type" : 302
},

1. Capture Rewrites

"rewrites" : [{
"source" : "/test/:capture/this.html",
"destination" : "/test/:capture/that.html"
},

Neither of these work as expected. Perhaps I'm doing something improperly or there is another means to accomplish the same?

Best,

Charles

Charles Aylward

unread,
Feb 8, 2018, 12:43:29 PM2/8/18
to Firebase Google Group
Did some more experimenting and need to make a correction regarding Redirect Glob Negation. Glob Negation works as expected on it's own. But when glob negation AND segment capturing are used together, redirects do not work as expected.

Working: 
"redirects": [ {
"source" : "/test/one-!(a|b|c)/staticend.html",
"destination" : "/test/one/staticend.html",
"type" : 302
},

Not Working: 
"redirects": [ {
"source" : "/test/one-!(a|b|c)/:dynamicend.html",
"destination" : "/test/one/:dynamicend.html",
"type" : 302
},

Michael Bleigh

unread,
Feb 8, 2018, 2:54:30 PM2/8/18
to fireba...@googlegroups.com
Glob pattern matching works differently using capture groups, which are only available for redirects. This is an inconsistency that is more or less a bug, but it's not on the team's immediate radar to fix.

Do you have a concrete, important use case that is blocked by this behavior? In general, we've found that rewrites don't have the same need for capture groups as redirects and advanced glob patterns have been a bit problematic overall.

Thanks,
Michael

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/3dad3a98-5748-4a26-ad64-19f3f087cd82%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Charles Aylward

unread,
Feb 9, 2018, 12:19:31 AM2/9/18
to Firebase Google Group
I'm attempting to use the firebase.json configuration to reroute language urls. Using rewrites, I'd like to implement the following priority order of redirection/rewrites:

1. /some/saved/path/zh-HANS-CN/test.html
2. /some/saved/path/zh-CN/test.html
3. /some/saved/path/zn/test.html
4. /some/saved/path/en/test.html

If a request for /some/saved/path/zh-XYZ/test.html comes in, I'd like it and all other region (XYZ portion) variations I don't support to use the /some/saved/path/zh/test.html page content, and if that doesn't exist then use the 'en' content.. That means my firebase.json Rewrite rules would look like:

"rewrites" : [{
"source" : "/:somepath*/zh-!(CN)/:somepage.html",
"destination" : "/:somepath*/zh/:somepage.html"
}, {
"source" : "/:somepath*/*/:somepage.html",
"destination" : "/:somepath*/en/:somepage.html"
}]

Note that the beginning path and the end page/path may vary. I'd appreciate any implementation suggestions you have.

Best,

Charles

Michael Bleigh

unread,
Feb 9, 2018, 1:12:53 PM2/9/18
to fireba...@googlegroups.com
That's an interesting use case. The only solution I can think of right now would be to rewrite the URL to a Cloud Function that would redirect appropriately. It should be relatively simple, as an exact-match of static content will always trump a rewrite.

We may revisit how our URL patterns work in the future (it's likely, but a ways off). I'll certainly take this feedback into consideration when we do.

Cheers,
Michael

Charles Aylward

unread,
Feb 9, 2018, 2:11:11 PM2/9/18
to Firebase Google Group
Cloud Functions is the ideal solution but we are unable to utilize that Firebase feature due to legal reasons *rolls eyes*

Instead, we are making use of redirection via JavaScript. Unfortunately, that means sending at least 2 requests to the server to get to the proper page (1st for the Javascript, 2nd for the redirection). And it means we are performing the redirection code on the client-side rather than server-side.

Thanks for your input.

Best,
Charles

Michael Bleigh

unread,
Feb 9, 2018, 4:25:41 PM2/9/18
to fireba...@googlegroups.com
If you expect to have users hitting this frequently (more than just on first page load) you could install a service worker that does the intelligent redirection on first page load. It wouldn't solve the problem if users tend to only visit one page, but may be worth exploring.

Trevor Hartman

unread,
Aug 16, 2023, 2:08:46 PM8/16/23
to Firebase Google Group
Checking back in on this - was this work ever prioritized?

From reading the docs I assumed a rewrite rule like:

      {
        "source": "/*/admin/:path",
        "destination": "/[network]/admin/:path.html"
      },


would work. However, it doesn't - and I found this thread while searching around for more examples.

The hint in the docs is "Capture URL segments for redirects" - aka not rewrites. I totally miss that detail every time I read the docs 😅

Sorry to revive a 5 year old thread.
Reply all
Reply to author
Forward
0 new messages