I am using Firebase Hosting with cleanUrls
.
Firebase Hosting also allows to enforce trailing slashes in all URLs with:
"hosting": {
"trailingSlash": true
}
This will force a 301 redirect if the user requests a page without using a trailing slash. However, I’d like to enforce this redirect only for directories (i.e.: for serving index.html
files).
According the documentation, you can leave trailingSlash
as undefined. This will:
- Serve
/foo/bar/page
with a 200 (/foo/bar/page.html
) - Serve
/foo/bar/
with a 200 (/foo/bar/index.html
) - Serve
/foo/bar
with a 200 (/foo/bar/index.html
)
The last part is unexpected, since I’d like to enforce trailing slashes for directories.
How can I enforce a 301 redirection for all directories to enforce a trailing slash only for them? In example:
- Serve
/foo/bar/page
with a 200 (/foo/bar/page.html
) - Serve
/foo/bar/
with a 200 (/foo/bar/index.html
) - Redirect
/foo/bar
with a 301 to/foo/bar/
Note there can be many directories so a solution that avoids writing a separate rule for each one is much preferred for maintainability.
Having /foo/bar
return a 404 would be okay too, but 301 is preferred.
2
Answers
Since Firebase Hosting’s configuration does not automatically differentiate between files and directories in the way traditional servers might, you could try and manually specify redirect rules for your known directory paths.
But:
Then (and this is not tested) you would need to use Cloud Functions for Firebase or Firebase Hosting’s integration with Cloud Run to programmatically handle requests. That allows you to implement logic that checks if a requested path corresponds to a directory and enforce a trailing slash through redirection.
Using a Cloud Function, that function intercepts HTTP requests, checks if the request URL corresponds to a directory (by checking if it maps to an
index.html
file in your public directory), and redirects to the same URL with a trailing slash if so.A pseudo-code example would be:
Route requests through this Cloud Function will use the
rewrites
feature in yourfirebase.json
, allowing the function to process and redirect directory requests accordingly.Your
firebase.json
would need a rewrite rule to direct traffic to this function for handling:But: routing all requests through a Cloud Function could introduce latency: do check the potential performance impact.
And implementing logic with Cloud Functions or Cloud Run adds complexity to your project and may incur costs based on your usage, so you might have to do some Cloud FinOps.
This can be done using firebase redirects and the fact you can use a regex in the firebase.json file:
This works assuming that all files end in an extension and folders don’t so try to keep it that way.
Docs refrence