1. The problem I’m having:
Hi everyone, trying to use caddy for development internal sites and its work awesome with widely-used cms (like Wordpess), but i have in trouble with htaccess->to->Caddyfile transformations on some off the beaten track project
few days of attempts and searching in docs and forum but, sadly, i’m dont understand some moments, described in Caddifile as comments
2. Error messages and/or full log output:
Sometimes caddy runs, but app not works)
3. Caddy version:
2.6.4
4. How I installed and ran Caddy:
a. System environment:
x64, Manjaro
b. Command:
sudo caddy run
(Caddyfile exists in current folder)
c. Service/unit/compose file:
d. My complete Caddy config:
comments used as questions
internal.site:443 {
root * "{host}"
file_server
tls internal
encode zstd gzip
log {
output file "internal.api_error.log"
level ERROR
}
# 1. site structure is multiple subfolders with own settings, so i need route directives for every entrypoint?
route /app/folder-1/www/ {
# 2. rewrite all path to php files (not on sigle index.php entry). Its right form?
@pathToPHP file {
try_files {path}/ {path}.php =404
split_path .php
}
rewrite @pathToPHP {file_match.relative}
# 3. single path rewrite. Right?
rewrite /api /api.php {file_match.relative}
# 4. in htaccess example env variables (like APP_ROOT) used as part of rewrites. Can i use something like this one in Caddyfile?
@phpFiles path *.php
reverse_proxy @phpFiles "unix//var/run/php-fpm/php-fpm.sock" {
transport fastcgi {
split .php
env PHP_VALUE "display_errors=on
log_errors=on
expose_php=off"
env APP_ROOT "/relative-path-to-backend-app/www/"
env STORAGE_ROOT "/relative-path-to-storage/folder/www/"
env API_ROOT "../relative-path-to-api/www/"
env DEBUG_ENABLED "true"
# but how can I use it in rewrite?
}
}
}
route /app/folder-2/www/ {
# ...
}
route /app/folder-3/www/ {
# ...
}
}
e. My .htaccess
config:
Originally .htaccess
file is looks like that in every separate app folder (like /app/folder-1/www/
)
# set env variables (used in site code)
SetEnvIf Request_URI ".*" APP_ROOT="/relative-path-to-backend-app/www/"
SetEnvIf Request_URI ".*" STORAGE_ROOT="/relative-path-to-storage/folder/www/"
SetEnvIf Request_URI ".*" API_ROOT="../relative-path-to-api/www/"
SetEnvIf Request_URI ".*" DEBUG_ENABLED="true"
# strip ending spash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/+$ %{ENV:APP_ROOT}$1 [R=301,L]
# seach index file in current app folder
RewriteRule ^index\.(html|htm|php)$ %{ENV:SITE_ROOT} [R=301,L]
# single rewrite
RewriteCond %{REQUEST_URI} /(api)/([^/]+)?$
RewriteRule .* %{ENV:APP_ROOT}%1.php?%2 [L,QSA]
# search php file in current app root folder - for path without extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ %{ENV:APP_ROOT}$1.php [L,QSA]