By huntybunz
I’m using app platform for a site, and I want to set it up to redirect a subdomain to a folder of the main domain. e.g.:
foo.example.com => example.com/foo
how do I do this? I’ve already setup foo.example.com as a custom domain in my app, but I can’t figure out the next step. I tried to do it via CNAME, but that tells me that example.com/foo is an invalid alias
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hi there,
DNS can only map hostnames to IPs or other hostnames. It does not know anything about paths like /foo, so doing foo.example.com to example.com/foo purely with a CNAME generally is not possible.
Since you are on App Platform, this likely has to be handled at the application or proxy level, not in DNS.
One option, if you control the app code, is to check the Host header for foo.example.com and return a 301 redirect to https://example.com/foo. That approach works in most frameworks.
Another possible option is to route things internally so that requests coming to foo.example.com are served from /foo, but that usually requires more custom proxy logic and is not always needed.
As far as I know, App Platform does not currently have a built-in DNS-level feature to redirect a subdomain to a path on another domain. The URL rewrite feature is for paths on the same host, not for mapping different hosts.
So in practice, the custom domain setup plus an app-level redirect is usually the safest way to do this.
Heya, @huntybunz
You can’t do this with DNS. A CNAME can only point to a hostname, not a URL path, so example.com/foo will always fail.
You need to add a redirect inside your app on App Platform. Attach foo.example.com to the app, then in your app code or framework config return a 301 redirect to /foo.
Regards
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.