To pass all requests to a PHP script, "excepting" (…)

Hi,

I would like all requests (page, image, js, css, etc…) to be passed to a given PHP script which is located at “/index.php”, excepting for the content of the folders “/i/" , "/js/” and the root files “/favicon.ico” and “/robots.txt”

The following configuration “seems” to do what I want :

rewrite {

	if {dir} not /i/
	if {dir} not /js/
			
	if {file} not favicon.ico
	if {file} not robots.txt
	
	to /index.php

}

fastcgi / /var/run/php-fpm.sock php

So I was wondering, if this is the right way to do it, or if there is another method, or something better ?

Thank you,

Looks pretty good!

I’d definitely do some real testing first to make sure the output of {dir} and {file} matches what you’re expecting to test for, but off the top of my head, it looks correct.

I would usually rely on if {path} not_starts_with /i/, which achieves very nearly the same result, but yours should be slightly more precise. Whether the precision will ever matter, I couldn’t say.

Technically, you should be rewriting to /, not to /index.php (the former is the canonical way to request an index). I don’t remember off the top of my head whether or not Caddy will redirect loop the user if there’s a fastcgi in the mix, but certainly if the rewritten request finds its way to the static fileserver, it’ll 301 you to the canonical /, which will get rewritten to /index.php, which will 301… etc.

1 Like

Thank you very much for your reply!

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.