Handle_errors doesn't redirect

1. Output of caddy version:

Latest version

2. How I run Caddy:

i run caddy with docker

a. System environment:

Debian 11
Docker

b. Command:

docker stack deploy -c docker-compose.yml app

c. Service/unit/compose file:

# Web proxy for SSL termination
  caddy:
    #image: docker.io/caddy:latest
    image: caddy:latest
    restart: unless-stopped
    ports:
    # HTTP et HTTPS
      - "80:80"
      - "443:443"
    networks:
      - caddy
    volumes:
      - ./apps/caddy/Caddyfile:/etc/caddy/Caddyfile:ro  #fichier de configuration principal de Caddy
      - ./data/caddy:/data  #indispensable pour stocker les données de certificats
      - ./apps/publichtml:/srv:ro -->

d. My complete Caddy config:

{
    # email to generate a valid SSL certificate
    email mgagnant@neosaiyan.fr

    #HTTP/3 support
    servers {
        protocol {
            experimental_http3
        }
    }
}

# snippet de règle de réécriture, permet d'être appelé en import dans chacun des sites php
(rewriterule) {
#Règle de réécriture de l'accueil Ademe
@accueil path_regexp accueil ^/accueil.*
rewrite @accueil /solutions/index.php
#Règle de réécriture des dossiers Ademe
@solfolder path_regexp solfolder .*f([0-9]+).html
rewrite @solfolder /solutions/folder.php?folid={re.solfolder.1}
#Règle de réécriture des stage Ademe
@solstage path_regexp solstage .*s([0-9]+).html
rewrite @solstage /solutions/stage.php?folid=26&stageid={re.solstage.1}
#Règle de réécriture des sessions (formulaire d'inscription) Ademe
@solinscr path_regexp solinscr .*r([0-9]+).html
rewrite @solinscr /solutions/beforeregister.php?sessionid={re.solfolder.1}
#Règle de réécriture recherche de formation (searchstage) Ademe
@searchstage path_regexp searchstage .*recherche-de-formation.html
rewrite @searchstage /solutions/searchstage.php
#Règle de réécriture contactez-nous (contact) Ademe
@contact path_regexp contact .*contactez-nous.html
rewrite @contact /solutions/contact.php
@contact-thankyou path_regexp contact-thankyou .*remerciements-contact.html
rewrite @contact-thankyou /solutions/contact-thankyou.php
#Règle de réécriture plan du site (sitemap) Ademe
@sitemap path_regexp sitemap .*plan-du-site.html
rewrite @sitemap /solutions/sitemap.php


#Règle de réécriture formulaire d'inscription (register & co) Ademe
# Voir l'envoi de variable en get pour rewriting entre beforeregister => register ex :(secteur=2&sessionid=1292&formval=Valider)
# Voir les actions des form pour le rewriting
@forminsc path_regexp forminsc .*formulaire-d-inscription.html
rewrite @forminsc /solutions/beforeregister.php
@register path_regexp register .*participant.html
rewrite @register /solutions/register.php
@register2 path_regexp register2 .*responsable.html
rewrite @register2 /solutions/register2.php
@register3 path_regexp register3 .*societe.html
rewrite @register3 /solutions/register3.php
@register4 path_regexp register4 .*facturation.html
rewrite @register4 /solutions/register4.php
@register5 path_regexp register5 .*message.html
rewrite @register5 /solutions/register5.php
@register6 path_regexp register6 .*synthese.html
rewrite @register6 /solutions/register6.php
@register7 path_regexp register7 .*enregistrement-formulaire-d-inscription.html
rewrite @register7 /solutions/register7.php
@register-thankyou path_regexp register-thankyou .*remerciements.html
rewrite @register-thankyou /solutions/register-thankyou.php

#Règle de réécriture formulaire création d'un compte (account & co)
# Voir l'envoi de variable en get pour rewriting entre beforecreateaccount => createaccount ex : (secteur=2&email=jlemoine45d@webcompetence.com)
# Voir les actions des form pour le rewriting
@beforecreateaccount path_regexp beforecreateaccount .*creation-de-compte.html
rewrite @beforecreateaccount /solutions/beforecreateaccount.php
@createaccount path_regexp createaccount .*creation-compte-vos-informations.html
rewrite @createaccount /solutions/createaccount.php
@createaccount2 path_regexp createaccount .*creation-compte-societe.html
rewrite @createaccount2 /solutions/createaccount2.php
@createaccount3 path_regexp createaccount3 .*creation-compte-message.html
rewrite @createaccount3 /solutions/createaccount3.php
@createaccount4 path_regexp createaccount4 .*creation-compte-synthese.html
rewrite @createaccount4 /solutions/createaccount4.php
@createaccount5 path_regexp createaccount5 .*creation-compte-enregistrement.html
rewrite @createaccount5 /solutions/createaccount5.php
@createaccount-thankyou path_regexp .*creation-compte-remerciement.html
rewrite @createaccount-thankyou /solutions/createaccount-thankyou.php
#Rewrite FAQ
@searchfaq path_regexp searchfaq .*faq.html
rewrite @searchfaq /solutions/searchfaq.php
#Règle de réécriture de la FAQ
@faq path_regexp faq .*a([0-9]+).html
rewrite @faq /solutions/faq.php?faqid={re.faq.1}
}

# Site PHP
docker.neosaiyan.fr {
# On indique quel sera le dossier racine du serveur web
root * /srv
php_fastcgi http://172.17.0.1:9000
encode gzip zstd
file_server
import rewriterule
handle_errors {
        @404 {
            expression {http.error.status_code} == 404
        }
        rewrite @404 /notfound.html
        file_server
    }
}
}

3. The problem I’m having:

Redirect error page for 404 error doesn-'t work. I want to redirect also error 500 btw.
I have always error 500 from browser
Any help will be appreciated.

4. Error messages and/or full log output:

Paste logs/commands/output here.
USE THE PREVIEW PANE TO MAKE SURE IT LOOKS NICELY FORMATTED.

5. What I already tried:

I try to reproduce this:

6. Links to relevant resources:

Thanks in advance for your help
Micka

with this one:
try_files {path}.php {path} /notfound.html

redirect seems to be OK but the rewrite rule:
@accueil path_regexp accueil ^/accueil.*
rewrite @accueil /solutions/index.php
redirect to notfound.html also !

then when I browse : docker.neosaiyan.fr instead of redirect to docker.neosaiyan.fr/accueil.html , it redirects to notfound.html.

I progress but slowly

Micka

What do you mean by ‘doesn’t work’? What do you want to see, exactly? What do you see instead, exactly?

There are no redirects in your configuration at all, only rewrites. These are two very different things and it’s important to distinguish between them.

1 Like

When I wrote that I except that when a user browse a webpage that doesn’t exist, caddy redirect or rewrite the uri to /notfound.html

Anyway, when an error like 404 or 500 occured, I want Caddy redirect/rewrite (dunno the difference) to a specific page like notfound.html

Thanks

Rewriting happens behind the scenes without telling the client. With a rewrite from foo.txt to bar.txt:

  1. Client requests foo.txt.
  2. Caddy responds immediately with bar.txt.

Redirections instruct the client to change their request. With a redirect from foo.txt to bar.txt:

  1. Client requests foo.txt.
  2. Caddy responds: “Please make another request for bar.txt instead”
  3. Client requests bar.txt.
  4. Caddy responds with bar.txt.

So, you’ve told me what you expect, but now can you tell me exactly what request you made to Caddy and exactly how Caddy responded? Including as much specific detail as possible.

Here’s what we need to know:

What specific request did you make to Caddy with this configuration? :x:
What do you want to see, exactly? :white_check_mark: (You said, contents of /notfound.html)
What do you see instead, exactly? :x:

1 Like

Thanks again for your reply.

What specific request did you make to Caddy with this configuration?
–>When a user browse a webpage that doesn’t exist like : http://docker.neosaiyan.fr/test/test.html , I would like caddy redirect to a static page named /notfound.html

What do you see instead, exactly?
1- When I put in caddyfile :
handle_errors {
@404 {
expression {http.error.status_code} == 404
}
rewrite @404 /notfound.html
file_server
}
}
–>I have a : ERR_TOO_MANY_REDIRECTS

When I put in caddyfile:
try_files {path}.php {path} /notfound.html
–>the redirection to /notfound.html is ok when I browse http://docker.neosaiyan.fr/test/test.html
BUT the rewrite rule docker.neosaiyan.fr to docker.neosaiyan.fr/accueil.html is also redirect to /notfound.html

Thanks for you help. (and sorry for my bad english…)

me again,

solution is (if anyone need help on this subject) :

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

where notfound.html is a static page for the redirection

Micka

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