Making Caddy Handle WordPress Pretty Permalinks

I had been using Apache as my web server choice for many years. While it did its job very nicely, I was not happy with the overall speed of it. So I went looking for a new web server. Originally I looked at nginx as it is the most common alternative to Apache, but I personally found it a little too complicated to set up. Then I found Caddy.


(Image Source)

I found Caddy particularly appealing because it automatically does a lot of stuff I was manually telling Apache to do, especially when it came to TLS certificate management. I won’t talk about the switch in this blog post, but the result is I am now happily running Caddy.

The only problem I encountered was with WordPress. WordPress has the ability to display “pretty” permalinks, where GET parameters or file names are not visible in URLs. However, it seems to always assume Apache is in use, so it only makes available a .htaccess file for handling this type of permalink.

As expected, Caddy doesn’t do anything with it, so WordPress was stuck using GET parameters for identifying resources. I wanted to have my pretty permalinks back, so I needed the .htaccess file to be recreated in Caddyfile language. Here is the .htaccess file that WordPress generates:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

This behavior can be recreated surprisingly easy by adding the following text to the Caddyfile:

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

Once this was added and applied by restarting Caddy, WordPress was able to start using pretty permalinks again.

Leave a Reply