Try_files on sub directory

Version v2.4.6

I have a file structure that contains a login.php at the server root level and another one in a subdirectory. I would like Caddy to default to their respective login.php.

Using the try_files directive I can get it working for the root directory and partially for the subdirectory : when requesting server-name/subdir/ it finds the proper subdir/login.php but not its associated css file. See file structure below.

Caddyfile:

:80 {
        root * /var/www/pirard4
        file_server
        try_files {path} /login.php
        try_files m/{path} /m/login.php
        php_fastcgi unix//var/run/php/php7.4-fpm.sock
        log {
                output file /var/log/caddy/pirard.log
        }
}

My simplified server tree :

    β”œβ”€β”€ login.php
    β”œβ”€β”€ other-files.php
    β”œβ”€β”€ m
    β”‚   β”œβ”€β”€ css
    β”‚   β”‚   └── 03302049-mobile.css
    β”‚   β”œβ”€β”€ login.php
    β”‚   β”œβ”€β”€ other-files.php

Edit : log entry when requesting http://server-name/m/

$ jq '.common_log' pirard.log; echo '' > pirard.log 
"192.168.0.92 - - [31/Mar/2022:19:22:13 +0200] \"GET /m/ HTTP/1.1\" 200 1196"
"192.168.0.92 - - [31/Mar/2022:19:22:13 +0200] \"GET /m/css/03302049-mobile.css HTTP/1.1\" 200 1196"

That’s kinda complicated.

Modern PHP apps do all their own routing through the index.php file at the root. I strongly suggest you consider doing it this way instead of delegating the routing decisions to Caddy.

1 Like

Make sense. I’ll give it a try. Thanks for the reply.

JL

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