Redirect globs with negative selection - firebase

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.

Related

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.

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 - Infinite Redirects

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?

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".

Unexpected trailing slash behaviour with Firebase hosting

I'm seeing unexpected behaviour (at least as per me) with trailing slashes on Firebase Hosting. I expect Firebase to serve URLs with a trailing slash (except for actual files present in the hosting directory). For this I set trailingSlash to true in firebase.json.
This resulted in:
example.com redirect to example.com/index.html/
example.com/js/script.js redirect to example.com/js/script.js/
example.com/images/image.png redirect to example.com/images/image.png/
The expected behaviour would be:
example.com redirect to example.com/
example.com/js/script.js served as it is without any redirect
example.com/images/image.png served as it is without any redirect
The addition of trailing slash to actual file URLs makes the browser/server think script.js is a directory instead of file and results in a 404. The addition of index.html to the root simply isn't elegant.
Expecting cleanUrls to do the trick I set cleanUrls to true along with trailingSlash and immediately the site goes into an endless redirect loop and fails to load. How can I stop Firebase from adding "index.html" at the root and the trailing slash for the actual js or image assets?
Edit:
As requested by Frank here's the firebase.json I'm using:
{
"firebase" : "example",
"public" : "public",
"cleanUrls" : false,
"trailingSlash" : true,
"ignore" : [ "firebase.json", "**/.*", "**/node_modules/**" ],
"rewrites": [{
"source" : "**",
"destination" : "/index.html"
}],
"headers": [{
"source" : "**/*.#(eot|otf|ttf|ttc|woff|font.css)",
"headers" : [{
"key" : "Access-Control-Allow-Origin",
"value" : "*"
}]
}, {
"source" : "**/*.#(jpg|jpeg|gif|png)",
"headers" : [{
"key" : "Cache-Control",
"value" : "max-age=600"
}]
}, {
"source" : "**/*.#(html|js)",
"headers" : [{
"key" : "Cache-Control",
"value" : "max-age=300"
}]
}]
}
P.S.: I have tried setting both trailingSlash and cleanUrls to true and also tried setting these true/false individually.
https://firebase.google.com/docs/hosting/full-config#trailingslash
When true, it will redirect URLs to add a trailing slash. When false
it will redirect URLs to remove a trailing slash. When unspecified,
trailing slashes are used only for directory index files (e.g.
about/index.html).
Just do not add that option and it will only affect urls
As for your question, If you want to invoke the command "rewrites", Server can execute "rewirtes" command when he cannot find any index.html in your root directory.
The solution one:
you just put index.html in your root directory under your "public" folder. I switch to your public folder and deploy your server.
The solution two:
you put index.html under some folder which you can named whatever. Then use JSON format: "rewrites": [{ "source" : "**", "destination" : "/folderName/index.html" }], but you cannot put any index.html under your root folder.
The solution three:
you create root folder, put your JS/CSS/IMG/ folders and index.html under the root folder. Then you deploy server JSON file like this.
{
"firebase": "you app name",
"public": ".",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
I'm sure this is too late for Vinny, but I just ran into this same issue. Adding <base href='/'>to my html files prevented the extra unexpected trailing slash behavior for me.
"hosting": {
"public": "...",
"ignore": [...],
"trailingSlash": false
}
Simply add trailingSlash property and set it to false will remove the trailing slash when firebase redirects. Check out their details documentation here

Resources