Help in converting .htaccess rewrite rules

1. The problem I’m having:

I am trying to use logHappens together with Caddy. But the .htaccess file has some RewriteRules in which I can’t seem to convert to rewrite rules in the Caddyfile.

The .htaccess has the following content:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule    ^$    webroot/    [L]
    RewriteRule    (.*) webroot/$1    [L]

And I try to make this into a rewrite * /webroot/{uri} but this fails.
I am just starting with Caddy, so any help is very much appreciated

2. Error messages and/or full log output:

2024/03/20 16:06:52.313 DEBUG http.handlers.rewrite rewrote request {"request": {"remote_ip": "", "remote_port": "49627", "client_ip": "", "proto": "HTTP/2.0", "method": "GET", "host": "logs.example.domain", "uri": "/", "headers": {"Accept-Language": ["en-GB,en;q=0.9"], "Sec-Fetch-Dest": ["document"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"], "Sec-Fetch-Site": ["none"], "Cookie": [], "Accept-Encoding": ["gzip, deflate, br"], "Sec-Fetch-Mode": ["navigate"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2.1 Safari/605.1.15"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "h2", "server_name": "logs.example.domain"}}, "method": "GET", "uri": "/webroot//"}
2024/03/20 16:06:52.313 DEBUG http.handlers.file_server sanitized path join {"site_root": "/var/www/logHappens", "request_path": "/webroot//", "result": "/var/www/logHappens/webroot"}
2024/03/20 16:06:52.314 DEBUG http.handlers.file_server no index file in directory  {"path": "/var/www/logHappens/webroot", "index_filenames": ["index.html", "index.txt"]}
2024/03/20 16:06:52.314 DEBUG http.log.error.log1 {id=pwn5upcf2} fileserver.(*FileServer).notFound (staticfiles.go:629): HTTP 404 {"request": {"remote_ip": "", "remote_port": "49627", "client_ip": "", "proto": "HTTP/2.0", "method": "GET", "host": "logs.example.domain", "uri": "/", "headers": {"Sec-Fetch-Site": ["none"], "Cookie": [], "Accept-Encoding": ["gzip, deflate, br"], "Sec-Fetch-Mode": ["navigate"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2.1 Safari/605.1.15"], "Accept-Language": ["en-GB,en;q=0.9"], "Sec-Fetch-Dest": ["document"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "h2", "server_name": "logs.example.domain"}}, "duration": 0.000358374, "status": 404, "err_id": "pwn5upcf2", "err_trace": "fileserver.(*FileServer).notFound (staticfiles.go:629)"}

3. Caddy version:

v2.7.6 h1:w0NymbG2m9PcvKWsrXO6EEkY9Ru4FJK8uQbYcev1p3A=

4. How I installed and ran Caddy:

Through the website, followed the installation instructions for Debian, Ubuntu.

a. System environment:

Ubuntu Focal, running Caddy as systemd unit

b. Command:

systemctl start caddy

c. Service/unit/compose file:

# /lib/systemd/system/caddy.service
# caddy.service
# For using Caddy with a config file.
# Make sure the ExecStart and ExecReload commands are correct
# for your installation.
# See for instructions.
# WARNING: This service does not use the --resume flag, so if you
# use the API to make changes, they will be overwritten by the
# Caddyfile next time the service is restarted. If you intend to
# use Caddy's API to configure it, add the --resume flag to the
# `caddy run` command or use the caddy-api.service file instead.


ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile --force


d. My complete Caddy config:

  log default {
    format console
    output file /var/log/caddy/system.log
    exclude http.log.access
  servers {

(log) {
  log {
    format console
    output file /var/log/caddy/{args[0]}.access.log {
      roll_size 100mb
      roll_keep 7
      roll_keep_for 720h

:80 {
  root * /var/www/sandbox
  import log sandbox

logs.example.domain {
  root * /var/www/logHappens
  rewrite * /webroot/{uri}
  import log logs.example.domain

5. Links to relevant resources:

Apparently I forgot to enable the PHP FastCGI module for this. Now I at least get a visible page.

1 Like

Is there still an issue then? Or is it working?

There is still an issue, but not related to Caddy.

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