Need Help in Rewriting Rules

1. Caddy version (caddy version):

caddy:latest

2. How I run Caddy:

a. System environment:

docker in debian

b. Command:

docker-compose up -d

c. Service/unit/compose file:

Paste full file contents here.
Make sure backticks stay on their own lines,
and the post looks nice in the preview pane.

d. My complete Caddyfile or JSON config:

# On indique quel sera le dossier racine du serveur web
root * /srv
php_fastcgi php:9000
encode gzip zstd
file_server
#Règle de réécriture
#RewriteRule ^accueil.* /solutions/index.php
rewrite /accueil.* /solutions/index.php
#RewriteRule .*f([0-9]+).html /solutions/folder.php?folid=$1 [L]
rewrite /.*f([0-9]+).html /solutions/folder.php?folid={1}
#RewriteRule .*recherche-de-formation.html /solutions/searchstage.php [L]
rewrite /.*recherche-de-formation.html /solutions/searchstage.php 
#RewriteRule .*dossier-([0-9]+)-(.*).html /$2f$1.html [R=permanent,L]
rewrite /.*dossier-([0-9]+)-(.*).html /{2f}{1}.html [R=permanent,L]
#RewriteCond %{REQUEST_URI} /data/
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{HTTP_HOST} ^formation.example.fr$ [NC]
#RewriteRule ^(.*)$ https://formations.example.fr%{REQUEST_URI} [R=301,L]
 
}

3. The problem I’m having:

Hi, I need help with rewriting rules in Caddy. I try to rewrite these rules from htaccess (lines with # are from .htaccess):
#RewriteRule ^accueil.* /solutions/index.php
rewrite /accueil.* /solutions/index.php → dunno how replace the ^
#RewriteRule .*f([0-9]+).html /solutions/folder.php?folid=$1 [L]
rewrite /.*f([0-9]+).html /solutions/folder.php?folid={1} –>dunno if it is correct
#RewriteRule .*recherche-de-formation.html /solutions/searchstage.php [L]
rewrite /.*recherche-de-formation.html /solutions/searchstage.php –>dunno if it is correct
#RewriteRule .dossier-([0-9]+)-(.).html /$2f$1.html [R=permanent,L]
rewrite /.dossier-([0-9]+)-(.).html /{2f}{1}.html –>dunno if it is correct

And for these 4 next lines, I dunno how to process
#RewriteCond %{REQUEST_URI} /data/
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{HTTP_HOST} ^formation.example.fr$ [NC]
#RewriteRule ^(.*)$ https://formations.example.fr%{REQUEST_URI} [R=301,L]

Could you help me please ? i am a newby in Caddy and specially in rewriting rules.
Thx in advance,
Micka

4. Error messages and/or full log output:

no error messages

5. What I already tried:

It is indicated in number 3

6. Links to relevant resources:

and this site with this guy :

1 Like

Hi again for the 3 first rules it is OK.
But always need help for these ones
#RewriteRule .dossier-([0-9]+)-(. ).html /$2f$1.html [R=permanent,L] –>dunno how to put the R=permanent
#RewriteRule .exmpless-domaine-([0-9]+)-..html /solutions/recherche-de-examples.html?theme=$1 [R=permanent,L]
#RewriteCond %{REQUEST_URI} /data/
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_URI} /back/..(jpg|png|jpeg|pdf)$
#RewriteCond %{HTTP_HOST} ^formation.examples.fr$ [NC]
#RewriteRule ^(.
)$ https://formations.examples.fr%{REQUEST_URI} [R=301,L]

Thanks for your help.

Micka

1 Like

You need to use named matchers to define path_regexp matchers, that you can then apply to your rewrite directives.

See the docs for matcher syntax, and look at the path_regexp section on the same page to see how to use capture group placeholders.

Thx for your answer. I do this for the other ones.
but for one like this :slight_smile:
RewriteCond %{HTTP_HOST} ^formation.examples.fr$ [NC]*

I don´t knpw how to translate.

That might be a host matcher. But it’s unclear what that does, really. You probably don’t need that at all, because the host is already being matched by the site address.

Hello Francis,
Thanks for you answers :slight_smile:
I try to complete my Caddyfile and it seems to be ok. “seems” because when I browse the website https://docker.neosaiyan.fr I have a lot of redirection 302. Do you know if i mistake in my caddyfile ?
Maybe there is a way to clean cache (php ou caddy) ? I say that because if I delete all the rewriterules caddy redirect on {uri]accueil.html
thanks for your help.

here the start of my caddyfile:

{
	email mgagnant@neosaiyan.fr
	#HTTP/3 support
	servers {
		protocol {
			experimental_http3
		}
	}
}


docker.neosaiyan.fr {
	root * /srv
	php_fastcgi php:9000
	encode gzip zstd
	file_server

	#Règle de réécriture de l’accueil
	@accueil path_regexp accueil ^/accueil.*
	rewrite @accueil /solutions/index.php

	#Règle de réécriture des dossiers
	@solfolder path_regexp solfolder .*f([0-9]+).html
	rewrite @solfolder /solutions/folder.php?folid={re.solfolder.1}
}

I have another rewrite rules in caddyfile btw

Thx for your help

Micka.

What do you see when you make the request with curl -v? It will show the headers of the response, which will help with seeing what’s going on.

Also, what’s in your logs? Turn on the debug global option (put it before email) to see more details in the logs.

thx for your reply Francis,

here is my curl -v

  • Trying 85.170.255.241:443…
  • Connected to docker.neosaiyan.fr (85.170.255.241) port 443 (#0)
  • ALPN, offering h2
  • ALPN, offering http/1.1
  • successfully set certificate verify locations:
  • CAfile: /etc/ssl/certs/ca-certificates.crt
  • CApath: /etc/ssl/certs
  • TLSv1.3 (OUT), TLS handshake, Client hello (1):
  • TLSv1.3 (IN), TLS handshake, Server hello (2):
  • TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
  • TLSv1.3 (IN), TLS handshake, Certificate (11):
  • TLSv1.3 (IN), TLS handshake, CERT verify (15):
  • TLSv1.3 (IN), TLS handshake, Finished (20):
  • TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
  • TLSv1.3 (OUT), TLS handshake, Finished (20):
  • SSL connection using TLSv1.3 / TLS_AES_128_GCM_SHA256
  • ALPN, server accepted to use h2
  • Server certificate:
  • subject: CN=docker.neosaiyan.fr
  • start date: Jul 26 09:02:51 2022 GMT
  • expire date: Oct 24 09:02:50 2022 GMT
  • subjectAltName: host “docker.neosaiyan.fr” matched cert’s “docker.neosaiyan.fr
  • issuer: C=US; O=Let’s Encrypt; CN=R3
  • SSL certificate verify ok.
  • Using HTTP2, server supports multi-use
  • Connection state changed (HTTP/2 confirmed)
  • Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
  • Using Stream ID: 1 (easy handle 0x5590d90905c0)

GET / HTTP/2
Host: docker.neosaiyan.fr
user-agent: curl/7.74.0
accept: /

  • TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
  • Connection state changed (MAX_CONCURRENT_STREAMS == 250)!
    < HTTP/2 302
    < alt-svc: h3=“:443”; ma=2592000,h3-29=“:443”; ma=2592000
    < content-type: text/html; charset=UTF-8
    < location: ./accueil.html
    < server: Caddy
    < status: 302 Found
    < x-powered-by: PHP/7.4.30
    < content-length: 0
    < date: Tue, 26 Jul 2022 14:50:49 GMT
    <
  • Connection #0 to host docker.neosaiyan.fr left intact

in logs I have many response like this :slight_smile: {“level”:“info”,“ts”:1658847127.1332803,“logger”:“http.log.access”,“msg”:“handled request”,“request”:{“remote_ip”:“91.167.58.35”,“remote_port”:“28818”,“proto”:“HTTP/2.0”,“method”:“GET”,“host”:“docker.neosaiyan.fr”,“uri”:“/css/materialize/accueil.html”,“headers”:{“Sec-Fetch-Mode”:[“navigate”],“Accept-Encoding”:[“gzip, deflate, br”],“Cache-Control”:[“max-age=0”],“User-Agent”:[“Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36”],“Accept”:[“text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.9”],“Sec-Fetch-Dest”:[“document”],“Sec-Ch-Ua”:[“".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"”],“Accept-Language”:[“fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7”],“Cookie”:[],“Sec-Fetch-User”:[“?1”],“Sec-Ch-Ua-Mobile”:[“?0”],“Sec-Ch-Ua-Platform”:[“"Windows"”],“Upgrade-Insecure-Requests”:[“1”],“Sec-Fetch-Site”:[“none”]},“tls”:{“resumed”:true,“version”:772,“cipher_suite”:4865,“proto”:“h2”,“server_name”:“docker.neosaiyan.fr”}},“user_id”:“”,“duration”:0.012834906,“size”:0,“status”:302,“resp_headers”:{“X-Powered-By”:[“PHP/7.4.30”],“Location”:[“./accueil.html”],“Content-Type”:[“text/html; charset=UTF-8”],“Server”:[“Caddy”],“Alt-Svc”:[“h3=":443"; ma=2592000,h3-29=":443"; ma=2592000”],“Status”:[“302 Found”]}}

1 Like

This tells me the redirect is coming from your PHP app, not from Caddy.

You’ll need to dig into what the app is doing that would cause the redirect.

1 Like

Ok Thanks Francis,
I will investigate in php