Caddy v2 redirect one subdomain to another and modify the original URI

1. Caddy version (caddy version):

V2 Latest

2. How I run Caddy:

As a service

a. System environment:


3. The problem I’m having:

Trying to rewrite a subdomain to another subdomain and change/rewrite the URI

I have a domain:

I want to forward the domain to:

What is the correct way to do this?

4. What I already tried:

I tried using uri replace but I feel like I am missing something simple. {
    uri replace /wl/?id/ /index.php/s/

Turn on the debug global option, and take a look at Caddy’s logs. You should see logs about the rewrites Caddy is attempting to do, and the result the redirect.

I added this to top of caddy file:


I started caddy from the terminal with my caddy file and I am not seeing anything about the rewrites.

What exactly should I be looking for as I have searched the log for “write” and haven’t found anything.

Don’t do that, run it as a service, like usual. See here in the docs for how to see your logs when running as a service:

You should see logs with the message “rewrote request”.

I exported the logs from journalctl -u caddy --no-pager | less and searched through them and cannot find any “rewrite request” from my last runs.
Of course, I had triggered the same URL in the browser which should have fired in the logs.
Sorry, I am new to some of this debugging and trying to learn.

Also, should I be doing this change in or after it is passed to

Does the question mark cause issues?

You can hit Shift+G to jump to the bottom of the logs in less, and hit / to start a search, hit / and enter again to find the next result etc. ? to search backwards instead of forwards. Shift+F to follow/tail the output.

But if you’re not seeing the log, then it’s because Caddy didn’t actually do a rewrite.

Oh right :man_facepalming:

So this is an edgecase, uri replace will actually run separately on first the path, then the query, but not on both together as one string. So that means a replacement that tries to replace ? will never match because ? will not be contained in those strings (the path will be /wl/ and query will be id/ccug8DTWATo4hrCT56XDjypaq8XHi015, replace running on each of those separately)

So I think the correct thing to do for you instead would be:

rewrite * /index.php/s/{query}?
uri replace id/ ""

Kinda funky, but basically {query} will contain id/ccug8DTWATo4hrCT56XDjypaq8XHi015, and you append that to index.php/s/, and adding ? to the end should clear the query. Then you remove id/ from the path itself in a second step.

That’s what I think it should do anyway. Check the logs to verify.

1 Like

Thanks for your help!

What I actually did then was use {} I see now that I missed the = sign in the example URLs.

Example URL should have been like this:

After I figured that out I didn’t need any rewrites, only the redirect.

So my final code looked like this: {
    redir{} 301

Final result:


Okay that makes way more sense, that was a strange query. :clap:

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