Map a subdomain to a subfolder

1. The problem I’m having:

I have an HTTP upstream application which is served from a subfolder /foo. The application is aware that its served from a subfolder so some kind of “baseURL” is in place. The app is currently reachable from the intranet via http://IP-of-the-server/foo.

The goal is to make it reachable from the internet via https://foo.domain.tld.

I managed to get it working with https://foo.domain.tld/foo, but I want to get rid of the /foo path in the domain.

2. Error messages and/or full log output:

N/A

3. Caddy version:

v2.11.2

4. How I installed and ran Caddy:

  • apt install caddy
  • Run by systemd as described in the caddy documentation

a. System environment:

  • Ubuntu 24.04.4 LTS

b. Command:

N/A

c. Service/unit/compose file:

N/A

d. My complete Caddy config:

foo.domain.tld {
	route {
		rewrite / /foo{uri}
		reverse_proxy * http://IP {
			header_up Host {host}
			header_up X-Real-IP {remote_host}
		}
	}
}

5. Links to relevant resources:

I have read The "subfolder problem", OR, "why can't I reverse proxy my app into a subfolder?" but do not know how to turn this into a working config.

You can try if it works better for you if you change that line to rewrite /foo{uri}.

Please specify which software it is. The existence of a baseurl option does not mean the application supports being served from a subfolder – sometimes it’s just a variable that is prepended to links in a notification handler like email. Consulting the documentation of such a software and, if unsuccessful, maybe their support forums/issue tracker should yield more info on whether people tried it already and if so, what worked for them.

As the linked thread specifies, HTML filtering/header rewriting is advanced stuff, and I would exhaust the easier solutions first before diving head first into some kind of config somebody posts here while you don’t understand what is happening. :slight_smile:

Its a custom-made PHP application which runs behind an apache on the intranet.

I am not sure if there is a baseURL option at all in that application. All I can say, that it runs for years and is served from a subfolder /foo and all assets are loaded correctly.

Same here. But even the easier solution like Option 1 and Option 2A doesn’t click with me without an example.

Since the app already lives inside a subfolder and is aware of that fact, I assumed that Option 1 is the way to go. But as I want to have just map a subdomain to a subfolder, I have the idea that Option 2A makes more sense.

For the records: I got it working with this config:

foo.domain.tld {
	handle {
		rewrite /* /foo{uri}
		reverse_proxy * http://IP {
			header_up Host {host}
			header_up X-Real-IP {remote_host}
		}
	}
}

From my understanding what it does: When a user hits https://foo.domain.tld the block appends foo and reverse proxy that to the IP, so as a result the request gets transformed into http://IP/foo.

Thanks for your answers, which lead eventually to a working solution.

1 Like

That’s interesting because exactly that solution didn’t work for most software for me.. but maybe it’s really about the support for being served from a subdirectory. So I stopped ever bothering with rewrites and all new webapps are always served via dedicated subdomain.

This was fruitful even for me, so let me thank you as well. :slight_smile: