Rewriting with http queries and path

1. The problem I’m having:

I want to rewrite an URL who contain http query to another URL who replace the query by a directory.
I have a URL like this:
/main/view/servers.php?server_digest=$1 and I want to rewrite it to /main/view/servers/server_digest. I tried this:
1 - rewrite /main/view/servers/* /main/view/servers.php?server_digest=$1
2 - rewrite /main/view/servers/(.) /main/view/servers.php?server_digest=$1
3 - rewrite /main/view/servers/(.
) /main/view/servers.php?server_digest={query}

I have the same problem with others path

2. Error messages and/or full log output:

mars 23 16:23:24 dedicated-server-05 caddy[1160708]: {"level":"info","ts":1679585004.800531,"logger":"tls","msg":"cleaning storage unit","description":"FileStorage:/var/lib/caddy/.local/share/caddy"}
mars 23 16:23:24 dedicated-server-05 caddy[1160708]: {"level":"info","ts":1679585004.8013365,"logger":"tls","msg":"finished cleaning storage units"}
mars 23 17:08:15 dedicated-server-05 systemd[1]: Stopping Caddy...
mars 23 17:08:15 dedicated-server-05 caddy[1160708]: {"level":"info","ts":1679587695.005596,"msg":"shutting down apps, then terminating","signal":"SIGTERM"}
mars 23 17:08:15 dedicated-server-05 caddy[1160708]: {"level":"warn","ts":1679587695.0056965,"msg":"exiting; byeee!! 👋","signal":"SIGTERM"}
mars 23 17:08:15 dedicated-server-05 caddy[1160708]: {"level":"info","ts":1679587695.0058038,"logger":"tls.cache.maintenance","msg":"stopped background certificate maintenance","cache":"0xc0004fd3b0"}
mars 23 17:08:15 dedicated-server-05 caddy[1160708]: {"level":"info","ts":1679587695.0058901,"logger":"admin","msg":"stopped previous server","address":"localhost:2019"}
mars 23 17:08:15 dedicated-server-05 caddy[1160708]: {"level":"info","ts":1679587695.0058987,"msg":"shutdown complete","signal":"SIGTERM","exit_code":0}
mars 23 17:08:15 dedicated-server-05 systemd[1]: caddy.service: Succeeded.
mars 23 17:08:15 dedicated-server-05 systemd[1]: Stopped Caddy.
mars 23 17:08:15 dedicated-server-05 systemd[1]: caddy.service: Consumed 21.450s CPU time.
mars 23 17:08:15 dedicated-server-05 systemd[1]: Starting Caddy...
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: caddy.HomeDir=/var/lib/caddy
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: caddy.AppDataDir=/var/lib/caddy/.local/share/caddy
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: caddy.AppConfigDir=/var/lib/caddy/.config/caddy
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: caddy.ConfigAutosavePath=/var/lib/caddy/.config/caddy/autosave.json
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: caddy.Version=v2.6.4 h1:2hwYqiRwk1tf3VruhMpLcYTg+11fCdr8S3jhNAdnPy8=
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: runtime.GOOS=linux
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: runtime.GOARCH=amd64
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: runtime.Compiler=gc
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: runtime.NumCPU=4
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: runtime.GOMAXPROCS=4
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: runtime.Version=go1.20
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: os.Getwd=/
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: LANG=fr_FR.UTF-8
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: NOTIFY_SOCKET=/run/systemd/notify
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: HOME=/var/lib/caddy
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: LOGNAME=caddy
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: USER=caddy
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: INVOCATION_ID=aa5207771d544422a3ef36f39b0ce784
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: JOURNAL_STREAM=8:804662657
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: {"level":"info","ts":1679587695.040275,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":""}
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: {"level":"warn","ts":1679587695.044272,"msg":"Caddyfile input is not formatted; run the 'caddy fmt' command to fix inconsistencies","adapter":"caddyfile","file":"/etc/caddy/Caddyfile","line":2}
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: {"level":"info","ts":1679587695.046815,"logger":"admin","msg":"admin endpoint started","address":"localhost:2019","enforce_origin":false,"origins":["//localhost:2019","//[::1]:2019","//127.0.0.1:2019"]}
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: {"level":"info","ts":1679587695.0472965,"logger":"tls.cache.maintenance","msg":"started background certificate maintenance","cache":"0xc000446d20"}
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: {"level":"info","ts":1679587695.0528343,"logger":"http.log","msg":"server running","name":"srv0","protocols":["h1","h2","h3"]}
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: {"level":"info","ts":1679587695.0529838,"logger":"tls","msg":"cleaning storage unit","description":"FileStorage:/var/lib/caddy/.local/share/caddy"}
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: {"level":"info","ts":1679587695.0530102,"logger":"tls","msg":"finished cleaning storage units"}
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: {"level":"info","ts":1679587695.0530236,"msg":"autosaved config (load with --resume flag)","file":"/var/lib/caddy/.config/caddy/autosave.json"}
mars 23 17:08:15 dedicated-server-05 systemd[1]: Started Caddy.
mars 23 17:08:15 dedicated-server-05 caddy[1417654]: {"level":"info","ts":1679587695.0542378,"msg":"serving initial configuration"}
(END)

No error appears in the logs.

3. Caddy version:

v2.6.4 h1:2hwYqiRwk1tf3VruhMpLcYTg+11fCdr8S3jhNAdnPy8=

4. How I installed and ran Caddy:

official installation methods found at Install — Caddy Documentation.

a. System environment:

Debian 11 (bullseye) x86_64

b. Command:

systemctl start caddy

d. My complete Caddy config:

:1080 {
    header / Cache-Control "no-cache, no-store, must-revalidate"

    root * /usr/share/nginx/dark-utilities.com

    #tls /etc/nginx/ssl/dark-utilities.com.pem /etc/nginx/ssl/dark-utilities.com.key {
    #    protocols tls1.3
    #}

    file_server

    php_fastcgi unix//run/php/php8.1-fpm.sock

    log {
        output file /var/log/caddy/access.log
    }

    rewrite / /main.html
    rewrite /referral/* /referral.html?referral=$1
    rewrite /recovery /recovery.php
    rewrite /abuse /abuse.html
    rewrite /privacy /privacy.html
    rewrite /terms /terms.html
    rewrite /main/manager /main/manager.php
    rewrite /main/premium /main/premium.php
    rewrite /main/api-access /main/api-access.php
    rewrite /main/rules /main/rules.php
    rewrite /main/referral /main/referral.php
    rewrite /main/post-exploitation /main/post-exploitation.php
    rewrite /main/tickets /main/tickets.php
    rewrite /main/documentation /main/documentation.php
    rewrite /main/advertisement /main/advertisement.php
    rewrite /main/admin/tickets /main/admin/tickets.php
    rewrite /main/admin/members /main/admin/members.php
    rewrite /main/admin/referral /main/admin/referral.php
    rewrite /main/admin/settings /main/admin/settings.php
    rewrite /main/view/servers/* /main/view/servers.php?server_digest=$1
    rewrite /main/view/tickets/* /main/view/tickets.php?ticket_uid=$1
    rewrite /api/v1/api-access /api/v1/api-access.php
    rewrite /api/v1/report /api/v1/report.php
    rewrite /api/v1/nodes-status /api/v1/nodes-status.php
    rewrite /api/v1/miners-status /api/v1/miners-status.php
    rewrite /api/v1/builds /api/v1/builds.php
    rewrite /api/v1/science /api/v1/science.php
    rewrite /api/v1/servers/evaluate/* /api/v1/servers-folder/evaluate.php?server_digest=$1
    rewrite /api/v1/servers/give/* /api/v1/servers-folder/give.php?server_digest=$1
    rewrite /api/v1/servers/shell-stdout/* /api/v1/servers-folder/shell-stdout.php?server_digest=$1
    rewrite /api/v1/servers/* /api/v1/servers.php?server_digest=$1
    rewrite /api/v1/servers /api/v1/servers.php
    rewrite /api/v1/webhooks/* /api/v1/webhooks.php?query=$1
    rewrite /api/v1/manager/attacks /api/v1/manager-folder/attacks.php
    rewrite /api/v1/manager /api/v1/manager.php
    rewrite /api/v1/tickets/reply/* /api/v1/tickets-folder/reply.php?ticket_uid=$1
    rewrite /api/v1/tickets/* /api/v1/tickets.php?ticket_uid=$1
    rewrite /api/v1/tickets /api/v1/tickets.php
    rewrite /api/v1/admin/tickets /api/v1/admin/tickets.php
    rewrite /api/v1/admin/members/* /api/v1/admin/members.php?user=$1
    rewrite /api/v1/admin/members /api/v1/admin/members.php
    rewrite /api/v1/admin/referral /api/v1/admin/referral.php
    rewrite /api/v1/users/wallet /api/v1/users-folder/wallet.php
    rewrite /api/v1/users/two-factor /api/v1/users-folder/two-factor.php
    rewrite /api/v1/users/api /api/v1/users-folder/api.php
    rewrite /api/v1/auth/two-factor /api/v1/auth/two-factor.php
    rewrite /api/v1/referral /api/v1/referral.php
    rewrite /api/v1/@me /api/v1/@me.php
    rewrite /api/v1/premium /api/v1/premium.php
    rewrite /api/v1/ssh-auto-link /api/v1/ssh-auto-link.php
    rewrite /api/v1/vector-map /api/v1/vector-map.php
    rewrite /api/v1/notifications /api/v1/notifications.php

    route /auth/login {
        try_files {path} /auth/login.php?{query}
    }

    route /auth/logout {
        try_files {path} /auth/logout.php
    }

    route /auth/two-factor {
        try_files {path} /auth/two-factor.php
    }

    route /main/vendor/* {
        rewrite * /error_files/401.html
        header Content-Type text/html
    }

    handle_errors {
        @404 expression {http.error.status_code} == 404
        @401 expression {http.error.status_code} == 401

        route @401 {
            rewrite * /error_files/401.html
            file_server
        }

        route @404 {
            rewrite * /error_files/404.html
            file_server
        }
    }
}

Try this:

@digest {
	path /main/view/servers.php
	query server_digest=*
}
rewrite @digest /main/view/servers/{query.server_digest}

For regexp matching, you should use the path_regexp matcher to capture parts of the path. Query params are available via placeholders.

You can probably replace most of these with try_files:

try_files {path}.php {path}.html {path} {path}/index.php /index.php

But I would strongly recommend doing the routing inside your PHP app itself via your index.php entrypoint. Any modern PHP framework makes this easy.

It’s returning 404

Well, there’s nothing I can do to help you with that. The 404 is coming from your upstream app. You’ll need to be more specific. Show details. What’s in your logs? Enable the debug global option in Caddy to see more detailed logs for the rewrites.

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