How to use this rewrite rule in Caddy V2

1. Caddy version (caddy version):

v2.0.0 h1:pQSaIJGFluFvu8KDGDODV8u4/QRED/OPyIR+MWYYse8=

2. How I run Caddy:

a. System environment:

OS: Debian 9.13 stretch
Kernel: x86_64 Linux 4.14
Uptime: 68d 22h 35m
Packages: 523
Shell: zsh 5.3.1
CPU: QEMU Virtual CPU version (cpu64-rhel6) @ 2.7GHz
RAM: 548MiB / 2013MiB

b. Command:

caddy run --config /etc/caddy/Caddyfile

d. My complete Caddyfile or JSON config:

domain {
	tls email
	root * /var/www/
	php_fastcgi  unix//tmp/php-cgi.sock

3. The problem I’m having:

I was able to convert the follow nginx configuration

location / {  
    try_files $uri $uri/ /index.php$is_args$query_string;  

to caddy v1

    rewrite {
            to {path} {path}/ /index.php?{query}

But I have no idea how to do so in caddy v2

5. What I already tried:

rewrite * /index.php?{query}


try_files {path} {path}/ /index.php?{query}


try_files $uri $uri/ /index.php?{query}

The try_files logic is already built-in to the php_fastcgi directive. See the expanded form:

All you’re missing is file_server, which was implicit in Caddy v1, but needs to be explicitly enabled in Caddy v2.

Also, you’re using a pretty old version now, I strongly suggest you upgrade to the latest version of Caddy, v2.2.1

It works.
Thanks for helping.

1 Like

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