Firebase Hosting - Infinite Redirects - firebase

I want to use redirects option to redirect visitors to canonical page urls. For example:
https://example.com/page1/index.html should become https://example.com/page1/,
and
https://example.com/page2// should become https://example.com/page2/
After reading the documentation I created these redirection rules:
"redirects": [
{
"source": "/:url/index.html",
"destination": "/:url/",
"type": 301
},
{
"source": "/:url//",
"destination": "/:url/",
"type": 301
}
],
This successfully redirects /page/index.html to /page/, but after that it falls into infinite redirect loop. It seems that "source": "/:url//" matches both /page// and /page/.
What is the correct way to specify regex rule to match only double slashes?

Related

Firebase rewrite rule not triggering Firebase Function

I'm trying to trigger the following function "dynamicMetaTagsUpdate" I have my Firebase rewrite setup like so
"rewrites": [
{
"source": "chart/**",
"function": "dynamicMetaTagsUpdate"
},
{
"source": "**",
"destination": "/index.html"
}
]
My apps URL are, the first view https://viz.wiijii.co/#/create-chart
and where I want the function to run https://viz.wiijii.co/#/chart/?id=-MfJuHEtO2EQ3mpo682O
This is the generated chart but it's not working am I missing something
This is because you are using URL fragments in the URL (#). Everything after the # in the URL is never sent to the server and cannot be used as the basis of Firebase Hosting rewrites. Instead, the URL should be e.g. https://viz.wiijii.co/chart/?id=-MfJuHEtO2EQ3mpo682O

Remove Query params and ? from URL in firebase redirect

Does anybody know how to remove everything after the ? in a url within firebase?
I've tried just the redirect, then the rewrite on top of this.
"redirects": [
{ "type": 302, "source": "/testMenuBrowse.php?cat=36", "destination": "/" }
],
"rewrites": [
{ "type": 302, "source": "/testMenuBrowse{,/**}", "destination": "/" }
],
Whatever I do the end URL is http://www.example.com/?cat=36
It's not currently possible to strip query parameters when redirecting using Firebase Hosting. You might want to file a feature request for the functionality.

Redirect globs with negative selection

We have a problem with dealing redirects on firebase hosting.
We need to redirect from / path / to /ru, while sitemap.xml, robots.txt and other static files (if those files exist) should not be affected by redirect rule.
For redirects we use glob pattern config:
`
"redirects": [
{
"source": "/",
"destination": "/ru",
"type": 302
},
{
"source": "/:lang",
"destination": "/ru/:lang",
"type": 302
},
{
"source": "novosti/:lang",
"destination": "/ru/novosti/:lang",
"type": 302
}
],`
Expected behavior:
/ => /ru,
/some-existing-link => /ru/some-existing-link,
/sitemap.xml => /sitemap.xml,
/novosti/news-link => /ru/novosti/news-link,
/some-none-existing-link => /ru/some-none-existing-link/ => 404.html
Observed behavior:
/some-existing-link => 404.html
I tried to use /:Lang* as mentioned in docs, but it breacks serving of static assets, like robots.txt
Also tried to use negate selector, but no success.
Looking for solution how to send redirect to subfolder, only if there is no file in a requested path.
You can read more about Firebase Hosting on How to Configure Redirects here. Also something about Global Pattern Matching since you are aiming to change to the entire / ru route.

In firebase hosting configuration, how do I write a rule to redirect all sources except one while capturing the url segment?

Setting up a firebase hosting project, I'd like to redirect all requests except for one to another domain and retain the url segment in the redirect. For example example.com/page should redirect to www.example.com/page, while anything starting with example.com/ignore should not redirect.
In firebase.json, I wrote this rule to redirect any request not in the ignore path:
"redirects": [
{
"source": "/!(ignore)",
"destination": "https://www.example.com/",
"type": 302
}
]
This redirect works, however I want to capture any url segments in the redirect. I'm trying to followed the example from their docs where :post* is the url segment I want to retain, but I do not know how to write it in the redirect rules.
"hosting": {
// ...
"redirects": [ {
"source": "/blog/:post*", // captures the entire URL segment beginning at "post"
"destination": "https://blog.myapp.com/:post", // includes the entire URL segment identified and captured by the "source" value
"type": 301
}
I have also tried the following which works to capture the url segment in redirect, but also redirects my ignored path as well.
"redirects": [
{
"source": "/:path*",
"destination": "https://www.example.com/:path",
"type": 302
}
]
I've also tried the above snippet with the ignore folder added to the ignore rules and this did not work either.
{
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**",
"**/ignore/**"
],
...
}
I'd like to be able write a glob pattern to capture any path that doesn't match the ignore path and use that in the redirect, something like:
"source": "/!(ignore)**path**,
"destination: "https://www.example.com/**path**",
"type": 302

Firebase Hosting rewrite glob behaviour issue

I have set up a firebase hosting project with three pages and my custom domain setup . These pages get hit based on different paths in the url. The firebase.json file is as follows
{
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [ {
"source": "/main",
"destination": "/index.html"
},{
"source": "/waitlistview/**",
"destination": "/waitlistview.html"
},{
"source": "/mwaitlistview/**",
"destination": "/waitlistview-minimal.html"
}
],
"cleanUrls": true
}
}
Now when i try a url :
foodini.co.in/waitlistview
Instead of getting an error with 404 page, i still hit my page
Whereas if i hit the url:
foodini.co.in/mwaitlistview
I get the 404 page as i expect.
How to i make the first url to also go to 404 page
According to firebase hosting docs, Hosting first tries to map a file inside the public directory to the path in the url. Only if there is no such file will a rewrite take effect.
In my case, I had a file /waitlistview.html directly inside the public folder. Thus firebase hosting just mapped the url to that file even without a trailing slash. The rewrite never occurred and thus there was no 404 page
Also for anyone interested:
your default domain name directly maps to index.html without the need for any specific path
Also /waitlistview** does not match /waitlistview/Ahmedabad/xyz path via GLOB matching so please have a slash before **
add
"trailingSlash": false,
before the line
"cleanUrls": true
Reason is that "foodini.co.in/waitlistview" does not match "/waitlistview/", it would match "/waitlistview".

Resources