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
Related
I have a proyect in firebase, with two custom domains, as show de image firebase console.
With the fist one, colademonofrozen.cl/ I get index.html file OK, but with the second, www.colademonofrozen.cl/ I get 404 not found.
Some strange thing about this case is that www.colademonofrozen.cl/anything display index.html file OK.
This is the hosting configuration:
`
"hosting": {
"public": "dist",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "/api/**",
"function": "app",
"region": "us-central1"
},
{
"source": "/admin",
"destination": "/admin.html"
},
{
"source": "**",
"destination": "/index.html"
}
]
},
`
I worked a lot for this project, I really appreciate any ideas.
I also appreciate anyone confirming that the domain shows an error page, so the problem cannot be ignored if it starts working.
I confirmed that it is not a cache problem.
The subdomain is configured more than two weeks ago, is not time propagation problem.
I requested a redirect at my dns provider, from the subdomain www.colademonofrozen.cl/ to colademonofrozen.cl/, but is not posible if the domain points to Firebase.
To my bad luck, the back label of the product for which I created the project are already printed.
see back label!
I change following DNS record:
name
type
value
www.colademonofrozen.cl.
CNAME
colademonofrozen.cl
for:
name
type
value
www.colademonofrozen.cl.
A
199.36.158.100
Now works OK.
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.
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
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".
I want to use Firebase Hosting to host an angular application and I need to create a redirection to some old files in another URL.
According with the Firebase Documentation you can do basic redirections
"redirects": [ {
"source" : "/foo",
"destination" : "/bar",
"type" : 301
}, {
"source" : "/firebase/*",
"destination" : "https://www.firebase.com",
"type" : 302
} ]
But I need a wildcard redirection
"redirects": [ {
"source" : "/config/*",
"destination" : "//oldsiteurl/config/[match-request]",
"type" : 302
}]
So, basically I need that myapp.firebase.com/config/some.json redirects to //oldsiteurl/config/some.json. I have a lot of json files so I do not want to match file by file.
Did you know if this is possible?
Thanks!
For people landing on this page, it is now possible to have wildcards in the URLs:
Sometimes it is desirable to capture parts of the source URL of a redirect and re-use them in the destination. You can do this using a : prefix to identify the segment and an optional * after the name to indicate that it should capture the rest of the URL
(source)
"redirects": [
{
"source": "/subdomain/:random*",
"destination": "https://subdomain.myapp.com/:random*",
"type": 301
}
]
From the Firebase documentation on URL redirects (emphasis mine):
If a match is found, an HTTP redirect response is set with the "Location" header set to the static destination string, which can be a relative path or an absolute URL.
So it looks like the wild-carded part of the match is not carried over into the redirect.