Host WordPress in a Subfolder with Caddy 2

Views: 819
0 0
Read Time:58 Second

After fixing 500 errors with WordPress hosted with Caddy 2 after I upgraded php-fpm, I ran into another weird issue of not being able to access sitemap.xml generated from one of the SEO tools. My WordPress files are all under /var/www/html/blog. Even though my Caddy displays the WordPress site without problems, however, when I used SEO tools to generate sitemap.xml and try to access it via https://domain.com/blog/sitemap.xml, it would always return the index page without successfully showing the sitemap. This is due to how the default php_fastcgi behavior is set up in Caddy, which is not very friendly when the PHP app (WordPress in this example) is placed in a subdirectory.

Below is a working example Caddyfile. Here, ‘handle_path’ is used to trim the URI.

localhost {
	encode zstd gzip	
	root * /var/www/html

	####### WordPress ####### 	
	handle_path /blog/* {
                root * /var/www/html/blog
		php_fastcgi unix//run/php/php8.1-fpm.sock {
			root /var/www/html/blog
		}
		file_server
	}

	# Enable the static file server.
	file_server
}

Don’t forget to reload the Caddyfile to apply the changes

$ sudo systemctl reload caddy
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Leave a Reply