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]
</IfModule>

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": "84.26.105.192", "remote_port": "49627", "client_ip": "84.26.105.192", "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": "84.26.105.192", "remote_port": "49627", "client_ip": "84.26.105.192", "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 https://caddyserver.com/docs/install 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.

[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target
Requires=network-online.target

[Service]
Type=notify
User=caddy
Group=caddy
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile --force
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

d. My complete Caddy config:

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

(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
  file_server
}

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

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.