How to point wildcard subdomain to some /subfolder or /path?

I have an app where users can create unique usernames like domain.tld/username but I also want them to use the username as username.domain.tld and point it to domain.tld/username.

Is it possible with fly.io? I search everywhere but couldn’t find a way.

You can add wildcard domains on fly.io, see e.g. Limitations with respect to wildcard certs or domains per app? - #7 by Zane_Milakovic

Then, handle forward on within your application (or probably on some reverse proxy which you are using inside your image, like nginx).

as an example,

server {
    listen 80;
    server_name ~^(?<subdomain>.+)\.example\.com$;

    location / {
        rewrite ^ https://example.com/$subdomain permanent;
    }
}
1 Like

Thank you for your reply.

I am using Nextjs and deployed using fly deploy I know it uses docker but I have not installed any server application like nginx or caddy.

Is there any work around with just nextjs or do I need to figure how to install nginx for the nextjs app?

You can probably do something like this within your next.config.js (not sure it works, but try out, see documentation at next.config.js Options: redirects | Next.js)

module.exports = {
  async redirects() {
    return [
      {
        source: '/:path*',
        destination: '/:path*', 
        permanent: true,
        has: [
          {
            type: 'host',
            value: '(?<subdomain>.+)\\.example\\.com',
          },
        ],
      },
    ]
  },
}

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.