Testing file existence

I was in a situation in which I wanted to test for the existence of a file as a condition in a rewrite (see the end of this thread for background). I wanted to be able to write:

rewrite {
    r (.*)
    if_op or
    if {file} ends_with .php
    if {path}/index.php exists
    to /proxyphp/{1}
}

I have read various threads which show how to get around this by using the “to” parameter with multiple paths, but none of these were able to do what I actually wanted in this instance.

Depending on how the parsing of parameters works and what would be easiest, an alternative syntax might be:

if {path} contains index.php

Is this an extension which might be considered?

So if I’m understanding this right, where a multi-destination rewrite goes to the file you’re testing for, you want to rewrite to a different destination than the file you’re testing for.

Hmm, that’s an interesting conundrum. I’ll think on it, but my initial thought is that you’re right, it can’t be done at present, and I can’t think of any tricky way to get around it to proxy any indexes with PHP files like fastcgi does out of the box with the php preset.

One point of interest is that testing for file/directory existence is noted as an optional todo on this work-in-progress PR:

https://github.com/mholt/caddy/pull/1948

Might be worth adding your voice to that PR to encourage adding it.

Thanks - I’ve done that.

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