Rewrite Wildcard subdomains to subpaths

1. Caddy version (caddy version): v2.4.3 h1:Y1FaV2N4WO3rBqxSYA8UZsZTQdN+PwcoOcAiZTM8C0I=

(Plugins DNS-Cloudflare and DNS-Hetzner)

2. How I run Caddy:

I’m Using a Caddyfile to configure caddy.
caddy start
I will configure it to be a service if configuration is successful (right now i’m still using HAProxy)

a. System environment:

Ubuntu 20.04 VPS on (likely a Hetzner machine)

b. Command:

caddy start

Paste command here.

d. My complete Caddyfile or JSON config:

        acme_dns cloudflare ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

* {

        reverse_proxy localhost:8091

        @user {
                host *
                path *
        handle @user {
                rewrite *{}{uri}

3. The problem I’m having:

I am trying to achieve this:
I have a NextJS app which has subpaths /u/{username} for user controlled pages (small configurable shops).
However i want to enable users to get a subdomain via {username}
The app has multiple locales (for now its english and german, but will likely be more in the future).
The locales are prepended to the path like this: /en-US/u/{username} or generally /{locale}/u/{username}.
If no locale is specified, the default app locale (en-US) will be used (/u/{username} defaults to english).
Users can also create custom pages which append to the userpath (/u/{username}/{custom-page})

I’m trying to rewrite {username} hosts to →[/{locale}?]/u/{username}[/{custom-page}?].

So far my current config works when trying direct urls: → (invisible to the browser)

However entering just leads to a redirect to →

4. Error messages and/or full log output:

5. What I already tried:

I have tried to use different matchers for rewrites, eg.
if {path} not_starts_with {label2} or
if {path} not_starts_with {locale} \n if {path} not_starts_with {label2}
however i cannot get rid of the initial redirect :confused:

Any ideas?

rewrite only affects the path + query portion of the URI, not the domain/scheme. Try this:

@user host *
rewrite @user /s/{labels.2}{uri}

Those are Caddy v1 syntax. That syntax is not supported in Caddy v2.

unfortunately this does the exact same thing :confused:

For the root url, it redirects to {user}{user} instead of rewriting it as {user}

The other routes seem to work

EDIT: Thinking about it, i might have to solve this differently. Even if i can make it work, i guess the subdirectory problem for asset paths will be a pita to deal with latero n.

Thanks for your help though @francislavoie!

The better option is to check the hostname in your backend app instead of doing rewrites in Caddy for this. Caddy passes through the Host header from the original request. It depends what your backend looks like and what kind of options your routing layer has, but that’s typically how it’s done. I’ve not used nextjs yet so I can quite answer that now

Thank you i know how to do it differently. I was hoping this could work but rethinking it made me realize its easier to read the host headers in the long run.

This topic was automatically closed after 30 days. New replies are no longer allowed.