Ability to host different applications under / paths and map them to custom domains, rather than hosting them under subdomains

Most sites like App Engine and Heroku have the following URL:

https://my-app-name.herokuapp.com/foo

Rather than that:

https://herokuapp.com/my-app-name/foo

I wonder (a) why, and (b) if there is a way to support it. It seems that GitHub does it with your projects in a gh-pages repository. For example, after what I remember, with a deposit like this:

https://github.com/myuser/myrepo

I can add a CNAME record to my repository, perform a configuration on my DNS provider / manager elsewhere and end up with a custom domain such as:

myrepodomain.com

My question is the following. If it is possible to configure a server or system so that you map the applications to a path rather than a subdomain, so foo.com/myapp rather myapp.foo.com, so you can then take that subpath and map it to a custom domain, as GitHub apparently does in my example. If this is not possible, I wonder why not. I have the faint impression that cookies and sessions may be problematic in a given case or that SEO may be a problem, but there may be some solutions.

I would basically like to do:

https://foo.com/app1/some/path
https://foo.com/app2/another/path
https: //foo.com/app ...

And then for those who want a custom domain, I map them to what they want:

https://foo.com/app1 -> helloworld.com
https://foo.com/app2 -> foobar.com

You wonder if this is possible and, if so, how it is.