Rewrite rule to caddy 2 for DouPHP

I’m trying to deploy a DouPHP site using caddy 2 with php.
And the site is already working fine without URL rewriting enabled.
But I now want to use URL rewriting as well, but I don’t understand how I should go about writing it.
Thanks for checking out or replying to this thread

Here are the official DouPHP Apache and Nginx rewrite rules
## nginx rewrite rule from DouPHP official 
location / { 
 if (!-e $request_filename) {
  rewrite ^/m/index\.html$     /m/index.php last;
  rewrite ^/m/sitemap\.xml$    /m/sitemap.php last;
  rewrite ^/m/(.*)$     /m/include/route.php?route=$1 last; 
  rewrite ^/index\.html$     /index.php last;
  rewrite ^/sitemap\.xml$    /sitemap.php last;
  rewrite ^/(.*)$     /include/route.php?route=$1 last; 
 } 
}
## apache rewrite rule from DouPHP official
<FilesMatch "\.(bak|inc|lib|sh|tpl|lbi|dwt)$">
    # Below Apache 2.4
    Order Deny,Allow 
    Deny from all
 
    # Apache 2.4 and above
    # Require all denied
</FilesMatch>

RewriteEngine On
RewriteRule ^index\.html$    index\.php [L]
RewriteRule ^sitemap\.xml$    sitemap\.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)

RewriteRule ^([^?]*) include/route.php?route=$1 [L,QSA]

The following rules are what I wrote based on the official nginx rules.
But the following configuration file will redirect all visits to sitemap.php.

# my current caddy configuration
www.domain.com:80 {
  root * /var/www/html/aoneng

  # gzip
  encode gzip

  php_fastcgi unix//run/php-fpm/www.sock

  # 防止 php 檔案上傳,以免被植入後門
  respond /uploads/*.php 404


  try_files index.html /index.php
  try_files sitemap.xml /sitemap.php
  try_files {path} {path}/ /include/route.php?route=$1
 
 # static file server
  file_server
Here is an excerpt from the journal
Feb 23 08:40:16 instance-20210219-2043 caddy[234670]: {"level":"debug","ts":1614069616.0461495,"logger":"http.handlers.rewrite","msg":"rewrote request","request":{"remote_addr":"x.x.x.x:49538","proto":"HTTP/1.1","method":"GET","host":"www.domain.com","uri":"/","headers":{"Cookie":["PHPSESSID=12fmnrc0ufd3thoo6k2lv791nj"],"Dnt":["1"],"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"],"Accept-Language":["zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"],"Cache-Control":["max-age=0"],"Upgrade-Insecure-Requests":["1"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.68"],"Accept-Encoding":["gzip, deflate"]}},"method":"GET","uri":"/index.php"}
Feb 23 08:40:16 instance-20210219-2043 caddy[234670]: {"level":"debug","ts":1614069616.0462162,"logger":"http.handlers.rewrite","msg":"rewrote request","request":{"remote_addr":"x.x.x.x:49538","proto":"HTTP/1.1","method":"GET","host":"www.domain.com","uri":"/index.php","headers":{"Dnt":["1"],"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"],"Accept-Language":["zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"],"Cookie":["PHPSESSID=12fmnrc0ufd3thoo6k2lv791nj"],"Upgrade-Insecure-Requests":["1"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.68"],"Accept-Encoding":["gzip, deflate"],"Cache-Control":["max-age=0"]}},"method":"GET","uri":"/sitemap.php"}
Feb 23 08:40:16 instance-20210219-2043 caddy[234670]: {"level":"debug","ts":1614069616.048709,"logger":"http.reverse_proxy.transport.fastcgi","msg":"roundtrip","request":{"remote_addr":"x.x.x.x:49538","proto":"HTTP/1.1","method":"GET","host":"www.domain.com","uri":"/sitemap.php","headers":{"X-Forwarded-For":["x.x.x.x"],"Dnt":["1"],"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"],"Accept-Language":["zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"],"Cookie":["PHPSESSID=12fmnrc0ufd3thoo6k2lv791nj"],"X-Forwarded-Proto":["http"],"Cache-Control":["max-age=0"],"Upgrade-Insecure-Requests":["1"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.68"],"Accept-Encoding":["gzip, deflate"]}},"dial":"/run/php-fpm/www.sock","env":{"AUTH_TYPE":"","CONTENT_LENGTH":"","CONTENT_TYPE":"","DOCUMENT_ROOT":"/var/www/html/aoneng","DOCUMENT_URI":"/sitemap.php","GATEWAY_INTERFACE":"CGI/1.1","HTTP_ACCEPT":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9","HTTP_ACCEPT_ENCODING":"gzip, deflate","HTTP_ACCEPT_LANGUAGE":"zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6","HTTP_CACHE_CONTROL":"max-age=0","HTTP_COOKIE":"PHPSESSID=12fmnrc0ufd3thoo6k2lv791nj","HTTP_DNT":"1","HTTP_HOST":"www.domain.com","HTTP_UPGRADE_INSECURE_REQUESTS":"1","HTTP_USER_AGENT":"Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.68","HTTP_X_FORWARDED_FOR":"x.x.x.x","HTTP_X_FORWARDED_PROTO":"http","PATH_INFO":"","QUERY_STRING":"","REMOTE_ADDR":"x.x.x.x","REMOTE_HOST":"x.x.x.x","REMOTE_IDENT":"","REMOTE_PORT":"49538","REMOTE_USER":"","REQUEST_METHOD":"GET","REQUEST_SCHEME":"http","REQUEST_URI":"/","SCRIPT_FILENAME":"/var/www/html/aoneng/sitemap.php","SCRIPT_NAME":"/sitemap.php","SERVER_NAME":"www.domain.com","SERVER_PROTOCOL":"HTTP/1.1","SERVER_SOFTWARE":"Caddy/v2.3.0"}}
Feb 23 08:40:16 instance-20210219-2043 caddy[234670]: {"level":"debug","ts":1614069616.192065,"logger":"http.handlers.reverse_proxy","msg":"upstream roundtrip","upstream":"unix//run/php-fpm/www.sock","request":{"remote_addr":"x.x.x.x:49538","proto":"HTTP/1.1","method":"GET","host":"www.domain.com","uri":"/sitemap.php","headers":{"X-Forwarded-Proto":["http"],"X-Forwarded-For":["x.x.x.x"],"Dnt":["1"],"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"],"Accept-Language":["zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"],"Cookie":["PHPSESSID=12fmnrc0ufd3thoo6k2lv791nj"],"Cache-Control":["max-age=0"],"Upgrade-Insecure-Requests":["1"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.68"],"Accept-Encoding":["gzip, deflate"]}},"duration":0.143358911,"headers":{"X-Powered-By":["PHP/7.4.15"],"Expires":["Thu, 19 Nov 1981 08:52:00 GMT"],"Pragma":["no-cache"],"Cache-Control":["private"],"Content-Type":["application/xml; charset=utf-8"]},"status":200}

I think your problem is with these lines:

try_files does not support matchers, so I don’t think these make sense. I think you mean to do this:

  rewrite /index.html /index.php
  rewrite /sitemap.xml /sitemap.php

When you do rewrites, be aware that they will happen before php_fastcgi's built-in try_files, so yours happen first. It might be better to use the expanded form and modify it to your needs, since it seems like your app doesn’t do its routing through index.php.

I modified the configuration like below, and I access all pages in the root directory fine, but when I access the www.<domain>.com/m subdirectory it redirects to index.php in the root directory, and the www.<domain>.com/admin subdirectory as well.

After the configuration file I have attached some of the caddy logs.

don't use config
*.<domain>.com {
  root * /var/www/html/aoneng

  # gzip
  encode gzip

  # 防止 php 檔案上傳,以免被植入後門
  respond /uploads/*.php 404

  

  @mIndex {
    not file
    path_regexp ^/m/index\.html$
  }

  @mSitemap {
    not file
    path_regexp ^/m/sitemap\.xml$
  }

  @m {
    not file
    path_regexp ^/m/(.*)$
  }

  @pcIndex {
    not file
    path_regexp ^/index\.html$
  }

  @pcSitemap {
    not file
    path_regexp ^/sitemap\.xml$
  }

  @pc {
    not file
    path_regexp ^/(.*)$
  }

  rewrite @mIndex /m/index.php
  rewrite @mSitemap /m/sitemap.php
  rewrite @m /m/include/route.php?route={1}
  rewrite @pcIndex /index.php
  rewrite @pcSitemap /sitemap.php
  rewrite @pc /include/route.php?route={1}


  php_fastcgi unix//run/php-fpm/www.sock

  # static file server
  file_server
caddy log
2021/02/24 07:42:48.824	DEBUG	http.handlers.rewrite	rewrote request	{"request": {"remote_addr": "<ip_address>:31066", "proto": "HTTP/2.0", "method": "GET", "host": "www.<domain>.com", "uri": "/admin", "headers": {"Cdn-Loop": ["cloudflare"], "X-Forwarded-Proto": ["https"], "Accept-Language": ["zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 Edg/88.0.705.74"], "Cf-Connecting-Ip": ["<ip_address>"], "Cf-Request-Id": ["087496a7d5000036de7ebd5000000001"], "Accept-Encoding": ["gzip"], "Upgrade-Insecure-Requests": ["1"], "Sec-Fetch-Mode": ["navigate"], "Sec-Fetch-Dest": ["document"], "X-Forwarded-For": ["<ip_address>"], "Dnt": ["1"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"], "Sec-Fetch-Site": ["none"], "Sec-Fetch-User": ["?1"], "Cookie": ["__cfduid=df360e844e6703d2b92ab9b0a418f01081612606842; PHPSESSID=12fmnrc0ufd3thoo6k2lv791nj"], "Cf-Ipcountry": ["CN"], "Cf-Ray": ["62678d52e8e536de-LAX"], "Cf-Visitor": ["{\"scheme\":\"https\"}"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "h2", "proto_mutual": true, "server_name": "www.<domain>.com"}}, "method": "GET", "uri": "/include/route.php?route=%3Cnil%3E"}
2021/02/24 07:42:48.824	DEBUG	http.reverse_proxy.transport.fastcgi	roundtrip	{"request": {"remote_addr": "<ip_address>:31066", "proto": "HTTP/2.0", "method": "GET", "host": "www.<domain>.com", "uri": "/include/route.php?route=%3Cnil%3E", "headers": {"Cdn-Loop": ["cloudflare"], "X-Forwarded-Proto": ["https"], "Accept-Language": ["zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"], "Cf-Request-Id": ["087496a7d5000036de7ebd5000000001"], "Accept-Encoding": ["gzip"], "Upgrade-Insecure-Requests": ["1"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 Edg/88.0.705.74"], "Cf-Connecting-Ip": ["<ip_address>"], "X-Forwarded-For": ["<ip_address>, <ip_address>"], "Dnt": ["1"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"], "Sec-Fetch-Mode": ["navigate"], "Sec-Fetch-Dest": ["document"], "Cookie": ["__cfduid=df360e844e6703d2b92ab9b0a418f01081612606842; PHPSESSID=12fmnrc0ufd3thoo6k2lv791nj"], "Cf-Ipcountry": ["CN"], "Cf-Ray": ["62678d52e8e536de-LAX"], "Cf-Visitor": ["{\"scheme\":\"https\"}"], "Sec-Fetch-Site": ["none"], "Sec-Fetch-User": ["?1"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "h2", "proto_mutual": true, "server_name": "www.<domain>.com"}}, "dial": "/run/php-fpm/www.sock", "env": {"AUTH_TYPE":"","CONTENT_LENGTH":"","CONTENT_TYPE":"","DOCUMENT_ROOT":"/var/www/html/aoneng","DOCUMENT_URI":"/include/route.php","GATEWAY_INTERFACE":"CGI/1.1","HTTPS":"on","HTTP_ACCEPT":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9","HTTP_ACCEPT_ENCODING":"gzip","HTTP_ACCEPT_LANGUAGE":"zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6","HTTP_CDN_LOOP":"cloudflare","HTTP_CF_CONNECTING_IP":"<ip_address>","HTTP_CF_IPCOUNTRY":"CN","HTTP_CF_RAY":"62678d52e8e536de-LAX","HTTP_CF_REQUEST_ID":"087496a7d5000036de7ebd5000000001","HTTP_CF_VISITOR":"{\"scheme\":\"https\"}","HTTP_COOKIE":"__cfduid=df360e844e6703d2b92ab9b0a418f01081612606842; PHPSESSID=12fmnrc0ufd3thoo6k2lv791nj","HTTP_DNT":"1","HTTP_HOST":"www.<domain>.com","HTTP_SEC_FETCH_DEST":"document","HTTP_SEC_FETCH_MODE":"navigate","HTTP_SEC_FETCH_SITE":"none","HTTP_SEC_FETCH_USER":"?1","HTTP_UPGRADE_INSECURE_REQUESTS":"1","HTTP_USER_AGENT":"Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 Edg/88.0.705.74","HTTP_X_FORWARDED_FOR":"<ip_address>, <ip_address>","HTTP_X_FORWARDED_PROTO":"https","PATH_INFO":"","QUERY_STRING":"route=%3Cnil%3E","REMOTE_ADDR":"<ip_address>","REMOTE_HOST":"<ip_address>","REMOTE_IDENT":"","REMOTE_PORT":"31066","REMOTE_USER":"","REQUEST_METHOD":"GET","REQUEST_SCHEME":"https","REQUEST_URI":"/admin","SCRIPT_FILENAME":"/var/www/html/aoneng/include/route.php","SCRIPT_NAME":"/include/route.php","SERVER_NAME":"www.<domain>.com","SERVER_PROTOCOL":"HTTP/2.0","SERVER_SOFTWARE":"Caddy/v2.3.0","SSL_CIPHER":"TLS_AES_128_GCM_SHA256","SSL_PROTOCOL":"TLSv1.3"}}
2021/02/24 07:42:48.824	DEBUG	http.handlers.reverse_proxy	upstream roundtrip	{"upstream": "unix//run/php-fpm/www.sock", "request": {"remote_addr": "<ip_address>:31066", "proto": "HTTP/2.0", "method": "GET", "host": "www.<domain>.com", "uri": "/include/route.php?route=%3Cnil%3E", "headers": {"Sec-Fetch-Dest": ["document"], "X-Forwarded-For": ["<ip_address>, <ip_address>"], "Dnt": ["1"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"], "Sec-Fetch-Mode": ["navigate"], "Sec-Fetch-User": ["?1"], "Cookie": ["__cfduid=df360e844e6703d2b92ab9b0a418f01081612606842; PHPSESSID=12fmnrc0ufd3thoo6k2lv791nj"], "Cf-Ipcountry": ["CN"], "Cf-Ray": ["62678d52e8e536de-LAX"], "Cf-Visitor": ["{\"scheme\":\"https\"}"], "Sec-Fetch-Site": ["none"], "Cdn-Loop": ["cloudflare"], "X-Forwarded-Proto": ["https"], "Accept-Language": ["zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"], "Cf-Connecting-Ip": ["<ip_address>"], "Cf-Request-Id": ["087496a7d5000036de7ebd5000000001"], "Accept-Encoding": ["gzip"], "Upgrade-Insecure-Requests": ["1"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 Edg/88.0.705.74"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "h2", "proto_mutual": true, "server_name": "www.<domain>.com"}}, "duration": 0.000654896, "headers": {"Status": ["302 Found"], "X-Powered-By": ["PHP/7.4.15"], "Location": ["http://www.<domain>.com/"], "Content-Type": ["text/html; charset=UTF-8"]}, "status": 302}
2021/02/24 07:42:49.041	DEBUG	http.handlers.rewrite	rewrote request	{"request": {"remote_addr": "<ip_address>:31330", "proto": "HTTP/1.1", "method": "GET", "host": "www.<domain>.com", "uri": "/", "headers": {"Sec-Fetch-Dest": ["document"], "Accept-Language": ["zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"], "Cf-Connecting-Ip": ["<ip_address>"], "Connection": ["Keep-Alive"], "Cf-Ipcountry": ["CN"], "Dnt": ["1"], "Upgrade-Insecure-Requests": ["1"], "Sec-Fetch-User": ["?1"], "Cdn-Loop": ["cloudflare"], "Cf-Visitor": ["{\"scheme\":\"https\"}"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"], "Sec-Fetch-Site": ["none"], "Accept-Encoding": ["gzip"], "X-Forwarded-For": ["<ip_address>"], "Cf-Ray": ["62678d544cba36de-LAX"], "Sec-Fetch-Mode": ["navigate"], "Cookie": ["__cfduid=df360e844e6703d2b92ab9b0a418f01081612606842; PHPSESSID=12fmnrc0ufd3thoo6k2lv791nj"], "X-Forwarded-Proto": ["https"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 Edg/88.0.705.74"], "Cf-Request-Id": ["087496a8af000036de76989000000001"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "", "proto_mutual": true, "server_name": "www.<domain>.com"}}, "method": "GET", "uri": "/index.php"}
2021/02/24 07:42:49.041	DEBUG	http.reverse_proxy.transport.fastcgi	roundtrip	{"request": {"remote_addr": "<ip_address>:31330", "proto": "HTTP/1.1", "method": "GET", "host": "www.<domain>.com", "uri": "/index.php", "headers": {"Dnt": ["1"], "Upgrade-Insecure-Requests": ["1"], "Sec-Fetch-User": ["?1"], "Sec-Fetch-Dest": ["document"], "Accept-Language": ["zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"], "Cf-Connecting-Ip": ["<ip_address>"], "Cf-Ipcountry": ["CN"], "Cdn-Loop": ["cloudflare"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"], "Sec-Fetch-Site": ["none"], "Cf-Visitor": ["{\"scheme\":\"https\"}"], "Cf-Ray": ["62678d544cba36de-LAX"], "Sec-Fetch-Mode": ["navigate"], "Cookie": ["__cfduid=df360e844e6703d2b92ab9b0a418f01081612606842; PHPSESSID=12fmnrc0ufd3thoo6k2lv791nj"], "Accept-Encoding": ["gzip"], "X-Forwarded-For": ["<ip_address>, <ip_address>"], "Cf-Request-Id": ["087496a8af000036de76989000000001"], "X-Forwarded-Proto": ["https"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 Edg/88.0.705.74"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "", "proto_mutual": true, "server_name": "www.<domain>.com"}}, "dial": "/run/php-fpm/www.sock", "env": {"AUTH_TYPE":"","CONTENT_LENGTH":"","CONTENT_TYPE":"","DOCUMENT_ROOT":"/var/www/html/aoneng","DOCUMENT_URI":"/index.php","GATEWAY_INTERFACE":"CGI/1.1","HTTPS":"on","HTTP_ACCEPT":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9","HTTP_ACCEPT_ENCODING":"gzip","HTTP_ACCEPT_LANGUAGE":"zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6","HTTP_CDN_LOOP":"cloudflare","HTTP_CF_CONNECTING_IP":"<ip_address>","HTTP_CF_IPCOUNTRY":"CN","HTTP_CF_RAY":"62678d544cba36de-LAX","HTTP_CF_REQUEST_ID":"087496a8af000036de76989000000001","HTTP_CF_VISITOR":"{\"scheme\":\"https\"}","HTTP_COOKIE":"__cfduid=df360e844e6703d2b92ab9b0a418f01081612606842; PHPSESSID=12fmnrc0ufd3thoo6k2lv791nj","HTTP_DNT":"1","HTTP_HOST":"www.<domain>.com","HTTP_SEC_FETCH_DEST":"document","HTTP_SEC_FETCH_MODE":"navigate","HTTP_SEC_FETCH_SITE":"none","HTTP_SEC_FETCH_USER":"?1","HTTP_UPGRADE_INSECURE_REQUESTS":"1","HTTP_USER_AGENT":"Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 Edg/88.0.705.74","HTTP_X_FORWARDED_FOR":"<ip_address>, <ip_address>","HTTP_X_FORWARDED_PROTO":"https","PATH_INFO":"","QUERY_STRING":"","REMOTE_ADDR":"<ip_address>","REMOTE_HOST":"<ip_address>","REMOTE_IDENT":"","REMOTE_PORT":"31330","REMOTE_USER":"","REQUEST_METHOD":"GET","REQUEST_SCHEME":"https","REQUEST_URI":"/","SCRIPT_FILENAME":"/var/www/html/aoneng/index.php","SCRIPT_NAME":"/index.php","SERVER_NAME":"www.<domain>.com","SERVER_PROTOCOL":"HTTP/1.1","SERVER_SOFTWARE":"Caddy/v2.3.0","SSL_CIPHER":"TLS_AES_128_GCM_SHA256","SSL_PROTOCOL":"TLSv1.3"}}
2021/02/24 07:42:49.069	DEBUG	http.handlers.reverse_proxy	upstream roundtrip	{"upstream": "unix//run/php-fpm/www.sock", "request": {"remote_addr": "<ip_address>:31330", "proto": "HTTP/1.1", "method": "GET", "host": "www.<domain>.com", "uri": "/index.php", "headers": {"Sec-Fetch-User": ["?1"], "Sec-Fetch-Dest": ["document"], "Accept-Language": ["zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"], "Cf-Connecting-Ip": ["<ip_address>"], "Cf-Ipcountry": ["CN"], "Dnt": ["1"], "Upgrade-Insecure-Requests": ["1"], "Cdn-Loop": ["cloudflare"], "Cf-Visitor": ["{\"scheme\":\"https\"}"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"], "Sec-Fetch-Site": ["none"], "Cookie": ["__cfduid=df360e844e6703d2b92ab9b0a418f01081612606842; PHPSESSID=12fmnrc0ufd3thoo6k2lv791nj"], "Accept-Encoding": ["gzip"], "X-Forwarded-For": ["<ip_address>, <ip_address>"], "Cf-Ray": ["62678d544cba36de-LAX"], "Sec-Fetch-Mode": ["navigate"], "X-Forwarded-Proto": ["https"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 Edg/88.0.705.74"], "Cf-Request-Id": ["087496a8af000036de76989000000001"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "", "proto_mutual": true, "server_name": "www.<domain>.com"}}, "duration": 0.027706478, "headers": {"Pragma": ["no-cache"], "Cache-Control": ["private"], "Content-Type": ["text/html; charset=utf-8"], "X-Powered-By": ["PHP/7.4.15"], "Expires": ["Thu, 19 Nov 1981 08:52:00 GMT"]}, "status": 200}
2021/02/24 07:42:56.123	DEBUG	http.reverse_proxy.transport.fastcgi	roundtrip	{"request": {"remote_addr": "<ip_address>:42218", "proto": "HTTP/1.1", "method": "GET", "host": "www.<domain>.com", "uri": "/m/index.php", "headers": {"Cf-Request-Id": ["087496c45c000036de9d3ff000000001"], "X-Forwarded-Proto": ["https"], "Cf-Visitor": ["{\"scheme\":\"https\"}"], "Upgrade-Insecure-Requests": ["1"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 Edg/88.0.705.74"], "Sec-Fetch-Mode": ["navigate"], "Cf-Connecting-Ip": ["<ip_address>"], "Accept-Encoding": ["gzip"], "Dnt": ["1"], "Sec-Fetch-Dest": ["document"], "Cookie": ["__cfduid=df360e844e6703d2b92ab9b0a418f01081612606842; PHPSESSID=12fmnrc0ufd3thoo6k2lv791nj"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"], "Sec-Fetch-Site": ["none"], "Sec-Fetch-User": ["?1"], "Accept-Language": ["zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"], "Cdn-Loop": ["cloudflare"], "Cf-Ipcountry": ["CN"], "X-Forwarded-For": ["<ip_address>, <ip_address>"], "Cf-Ray": ["62678d809f9a36de-LAX"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "", "proto_mutual": true, "server_name": "www.<domain>.com"}}, "dial": "/run/php-fpm/www.sock", "env": {"AUTH_TYPE":"","CONTENT_LENGTH":"","CONTENT_TYPE":"","DOCUMENT_ROOT":"/var/www/html/aoneng","DOCUMENT_URI":"/m/index.php","GATEWAY_INTERFACE":"CGI/1.1","HTTPS":"on","HTTP_ACCEPT":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9","HTTP_ACCEPT_ENCODING":"gzip","HTTP_ACCEPT_LANGUAGE":"zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6","HTTP_CDN_LOOP":"cloudflare","HTTP_CF_CONNECTING_IP":"<ip_address>","HTTP_CF_IPCOUNTRY":"CN","HTTP_CF_RAY":"62678d809f9a36de-LAX","HTTP_CF_REQUEST_ID":"087496c45c000036de9d3ff000000001","HTTP_CF_VISITOR":"{\"scheme\":\"https\"}","HTTP_COOKIE":"__cfduid=df360e844e6703d2b92ab9b0a418f01081612606842; PHPSESSID=12fmnrc0ufd3thoo6k2lv791nj","HTTP_DNT":"1","HTTP_HOST":"www.<domain>.com","HTTP_SEC_FETCH_DEST":"document","HTTP_SEC_FETCH_MODE":"navigate","HTTP_SEC_FETCH_SITE":"none","HTTP_SEC_FETCH_USER":"?1","HTTP_UPGRADE_INSECURE_REQUESTS":"1","HTTP_USER_AGENT":"Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 Edg/88.0.705.74","HTTP_X_FORWARDED_FOR":"<ip_address>, <ip_address>","HTTP_X_FORWARDED_PROTO":"https","PATH_INFO":"","QUERY_STRING":"","REMOTE_ADDR":"<ip_address>","REMOTE_HOST":"<ip_address>","REMOTE_IDENT":"","REMOTE_PORT":"42218","REMOTE_USER":"","REQUEST_METHOD":"GET","REQUEST_SCHEME":"https","REQUEST_URI":"/m/index.php","SCRIPT_FILENAME":"/var/www/html/aoneng/m/index.php","SCRIPT_NAME":"/m/index.php","SERVER_NAME":"www.<domain>.com","SERVER_PROTOCOL":"HTTP/1.1","SERVER_SOFTWARE":"Caddy/v2.3.0","SSL_CIPHER":"TLS_AES_128_GCM_SHA256","SSL_PROTOCOL":"TLSv1.3"}}
2021/02/24 07:42:56.135	DEBUG	http.handlers.reverse_proxy	upstream roundtrip	{"upstream": "unix//run/php-fpm/www.sock", "request": {"remote_addr": "<ip_address>:42218", "proto": "HTTP/1.1", "method": "GET", "host": "www.<domain>.com", "uri": "/m/index.php", "headers": {"Upgrade-Insecure-Requests": ["1"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 Edg/88.0.705.74"], "Sec-Fetch-Mode": ["navigate"], "Cf-Connecting-Ip": ["<ip_address>"], "Accept-Encoding": ["gzip"], "Dnt": ["1"], "Sec-Fetch-Dest": ["document"], "Cookie": ["__cfduid=df360e844e6703d2b92ab9b0a418f01081612606842; PHPSESSID=12fmnrc0ufd3thoo6k2lv791nj"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"], "Sec-Fetch-Site": ["none"], "Sec-Fetch-User": ["?1"], "Accept-Language": ["zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"], "Cdn-Loop": ["cloudflare"], "Cf-Ipcountry": ["CN"], "X-Forwarded-For": ["<ip_address>, <ip_address>"], "Cf-Ray": ["62678d809f9a36de-LAX"], "Cf-Request-Id": ["087496c45c000036de9d3ff000000001"], "X-Forwarded-Proto": ["https"], "Cf-Visitor": ["{\"scheme\":\"https\"}"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "", "proto_mutual": true, "server_name": "www.<domain>.com"}}, "duration": 0.012638236, "headers": {"Location": ["https://www.<domain>.com/"], "Status": ["302 Found"], "X-Powered-By": ["PHP/7.4.15"], "Expires": ["Thu, 19 Nov 1981 08:52:00 GMT"], "Pragma": ["no-cache"], "Cache-Control": ["private"], "Content-Type": ["text/html; charset=utf-8"]}, "status": 302}
2021/02/24 07:42:56.773	DEBUG	http.handlers.rewrite	rewrote request	{"request": {"remote_addr": "<ip_address>:42218", "proto": "HTTP/1.1", "method": "GET", "host": "www.<domain>.com", "uri": "/", "headers": {"Sec-Fetch-Site": ["none"], "Accept-Language": ["zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"], "Cf-Connecting-Ip": ["<ip_address>"], "Cf-Ipcountry": ["CN"], "X-Forwarded-For": ["<ip_address>"], "Cf-Ray": ["62678d84baf436de-LAX"], "Upgrade-Insecure-Requests": ["1"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 Edg/88.0.705.74"], "Cf-Request-Id": ["087496c6f9000036dec4971000000001"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"], "Sec-Fetch-User": ["?1"], "Cookie": ["__cfduid=df360e844e6703d2b92ab9b0a418f01081612606842; PHPSESSID=12fmnrc0ufd3thoo6k2lv791nj"], "Accept-Encoding": ["gzip"], "X-Forwarded-Proto": ["https"], "Cf-Visitor": ["{\"scheme\":\"https\"}"], "Sec-Fetch-Mode": ["navigate"], "Connection": ["Keep-Alive"], "Dnt": ["1"], "Sec-Fetch-Dest": ["document"], "Cdn-Loop": ["cloudflare"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "", "proto_mutual": true, "server_name": "www.<domain>.com"}}, "method": "GET", "uri": "/index.php"}
2021/02/24 07:42:56.773	DEBUG	http.reverse_proxy.transport.fastcgi	roundtrip	{"request": {"remote_addr": "<ip_address>:42218", "proto": "HTTP/1.1", "method": "GET", "host": "www.<domain>.com", "uri": "/index.php", "headers": {"Upgrade-Insecure-Requests": ["1"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 Edg/88.0.705.74"], "Sec-Fetch-Site": ["none"], "Accept-Language": ["zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"], "Cf-Connecting-Ip": ["<ip_address>"], "Cf-Ipcountry": ["CN"], "X-Forwarded-For": ["<ip_address>, <ip_address>"], "Cf-Ray": ["62678d84baf436de-LAX"], "Cf-Request-Id": ["087496c6f9000036dec4971000000001"], "Cookie": ["__cfduid=df360e844e6703d2b92ab9b0a418f01081612606842; PHPSESSID=12fmnrc0ufd3thoo6k2lv791nj"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"], "Sec-Fetch-User": ["?1"], "Sec-Fetch-Mode": ["navigate"], "Accept-Encoding": ["gzip"], "X-Forwarded-Proto": ["https"], "Cf-Visitor": ["{\"scheme\":\"https\"}"], "Cdn-Loop": ["cloudflare"], "Dnt": ["1"], "Sec-Fetch-Dest": ["document"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "", "proto_mutual": true, "server_name": "www.<domain>.com"}}, "dial": "/run/php-fpm/www.sock", "env": {"AUTH_TYPE":"","CONTENT_LENGTH":"","CONTENT_TYPE":"","DOCUMENT_ROOT":"/var/www/html/aoneng","DOCUMENT_URI":"/index.php","GATEWAY_INTERFACE":"CGI/1.1","HTTPS":"on","HTTP_ACCEPT":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9","HTTP_ACCEPT_ENCODING":"gzip","HTTP_ACCEPT_LANGUAGE":"zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6","HTTP_CDN_LOOP":"cloudflare","HTTP_CF_CONNECTING_IP":"<ip_address>","HTTP_CF_IPCOUNTRY":"CN","HTTP_CF_RAY":"62678d84baf436de-LAX","HTTP_CF_REQUEST_ID":"087496c6f9000036dec4971000000001","HTTP_CF_VISITOR":"{\"scheme\":\"https\"}","HTTP_COOKIE":"__cfduid=df360e844e6703d2b92ab9b0a418f01081612606842; PHPSESSID=12fmnrc0ufd3thoo6k2lv791nj","HTTP_DNT":"1","HTTP_HOST":"www.<domain>.com","HTTP_SEC_FETCH_DEST":"document","HTTP_SEC_FETCH_MODE":"navigate","HTTP_SEC_FETCH_SITE":"none","HTTP_SEC_FETCH_USER":"?1","HTTP_UPGRADE_INSECURE_REQUESTS":"1","HTTP_USER_AGENT":"Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 Edg/88.0.705.74","HTTP_X_FORWARDED_FOR":"<ip_address>, <ip_address>","HTTP_X_FORWARDED_PROTO":"https","PATH_INFO":"","QUERY_STRING":"","REMOTE_ADDR":"<ip_address>","REMOTE_HOST":"<ip_address>","REMOTE_IDENT":"","REMOTE_PORT":"42218","REMOTE_USER":"","REQUEST_METHOD":"GET","REQUEST_SCHEME":"https","REQUEST_URI":"/","SCRIPT_FILENAME":"/var/www/html/aoneng/index.php","SCRIPT_NAME":"/index.php","SERVER_NAME":"www.<domain>.com","SERVER_PROTOCOL":"HTTP/1.1","SERVER_SOFTWARE":"Caddy/v2.3.0","SSL_CIPHER":"TLS_AES_128_GCM_SHA256","SSL_PROTOCOL":"TLSv1.3"}}
2021/02/24 07:42:56.800	DEBUG	http.handlers.reverse_proxy	upstream roundtrip	{"upstream": "unix//run/php-fpm/www.sock", "request": {"remote_addr": "<ip_address>:42218", "proto": "HTTP/1.1", "method": "GET", "host": "www.<domain>.com", "uri": "/index.php", "headers": {"Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"], "Sec-Fetch-User": ["?1"], "Cookie": ["__cfduid=df360e844e6703d2b92ab9b0a418f01081612606842; PHPSESSID=12fmnrc0ufd3thoo6k2lv791nj"], "Accept-Encoding": ["gzip"], "X-Forwarded-Proto": ["https"], "Cf-Visitor": ["{\"scheme\":\"https\"}"], "Sec-Fetch-Mode": ["navigate"], "Dnt": ["1"], "Sec-Fetch-Dest": ["document"], "Cdn-Loop": ["cloudflare"], "Cf-Connecting-Ip": ["<ip_address>"], "Cf-Ipcountry": ["CN"], "X-Forwarded-For": ["<ip_address>, <ip_address>"], "Cf-Ray": ["62678d84baf436de-LAX"], "Upgrade-Insecure-Requests": ["1"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 Edg/88.0.705.74"], "Sec-Fetch-Site": ["none"], "Accept-Language": ["zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"], "Cf-Request-Id": ["087496c6f9000036dec4971000000001"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "", "proto_mutual": true, "server_name": "www.<domain>.com"}}, "duration": 0.026487959, "headers": {"Pragma": ["no-cache"], "Cache-Control": ["private"], "Content-Type": ["text/html; charset=utf-8"], "X-Powered-By": ["PHP/7.4.15"], "Expires": ["Thu, 19 Nov 1981 08:52:00 GMT"]}, "status": 200}

Run caddy adapt --pretty to see how the rewrites are ordered in your config. The order of same-directives in the Caddyfile is not guaranteed when not using path matchers, because there’s no defined way to sort other kinds of matchers. You may need to wrap your rewrite directives in a route to preserve their order.

Btw {1} is not a valid placeholder in Caddy. Please see the matcher docs, you must give a name to your matcher and use a placeholder such as {re.<name>.1} instead.

Thanks for your help, after the changes all seems to function properly now.

It’s just that the config file seems a bit too verbose, I wonder if there’s a concise way to write it?

Verbose Config ``` www..com { root * /var/www/html/aoneng

gzip

encode gzip

防止 php 檔案上傳,以免被植入後門

respond /uploads/*.php 404

php_fastcgi unix//run/php-fpm/www.sock

@mIndex {
not file
path_regexp ^/m/index.html$
}

@mSitemap {
not file
path_regexp ^/m/sitemap.xml$
}

@m {
not file
path_regexp m ^/m/(.*)$
}

@pcIndex {
not file
path_regexp ^/index.html$
}

@pcSitemap {
not file
path_regexp ^/sitemap.xml$
}

@pc {
not file
path_regexp pc ^/(.*)$
}

rewrite @mIndex /m/index.php
rewrite @mSitemap /m/sitemap.php
rewrite @m /m/include/route.php?route={re.m.1}

rewrite @pcIndex /index.php
rewrite @pcSitemap /sitemap.php
rewrite @pc /include/route.php?route={re.pc.1}

static file server

file_server

}

Maybe like this:

@rewrites not file
route @rewrites {
	@mIndex path_regexp ^/m/index.html$
	rewrite @mIndex /m/index.php

	@mSitemap path_regexp ^/m/sitemap.xml$
	rewrite @mSitemap /m/sitemap.php

	...
}
1 Like

The documentation mentions that the regexp name is optional, how do I fill in the capture_group correctly if I don’t use the name

I can’t get the uri correctly in the following way

    @pc path_regexp ^/(.*)$
    rewrite @pc /include/route.php?route={re.1}

It’s optional in the sense that if you don’t need to use capture groups you can omit it. But it is required if you want to use capture groups.

1 Like

@francislavoie Thank you for your patience and help.

The final configuration is attached below

www.<domain>.com {
  root * /var/www/html/aoneng

  # gzip
  encode gzip

  # 防止 php 檔案上傳,以免被植入後門
  respond /images/upload/*.php 404

  php_fastcgi unix//run/php-fpm/www.sock

  # Url rewrite for DouPHP
  @rewrites not file
  route @rewrites {
    @mIndex path_regexp ^/m/index.html$
    rewrite @mIndex /m/index.php

    @mSitemap path_regexp ^/m/sitemap.xml$
    rewrite @mSitemap /m/sitemap.php

    @m path_regexp m ^/m/(.*)$
    rewrite @m /m/include/route.php?route={re.m.1}

    @pcIndex path_regexp ^/index.html$
    rewrite @pcIndex /index.php

    @pcSitemap path_regexp ^/sitemap.xml$
    rewrite @pcSitemap /sitemap.php

    @pc path_regexp r ^/(.*)$
    rewrite @pc /include/route.php?route={re.r.1}

  }

  # static file server
  file_server
}

1 Like

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