Search code examples
reverse-proxycaddycaddyfile

Caddy rewrite and redir


I have a tricky case. I want to redirect always exept in few case. I want to see "toto.png" when i request www.myWebsite.com/toto.png but also when i request www.myWebsite.com/titi or www.myWebsite.com/TITI. In all other case i want to redirect on www.anotherWebsite.com/.

Here my current configuration not working :/

www.myWebsite.com{
  tls [email protected]
  root /var/www/html/
  rewrite / /anotherWebsite
  rewrite /titi /toto.png
  redir /anotherWebsite https://www.anotherWebsite.com/
}

Solution

  • I found this possibility:

    www.myWebsite.com, myWebsite.com {
      tls [email protected]
      root /var/www/html/website
      rewrite {
        if {path} is /titi 
        if {path} is /TITI
        if_op  or
        to /toto.png
      }
      rewrite {
        # Check for a file, then a folder
        # If neither exists, we would usually issue a 404
        # Instead, here we rewrite to /anotherWebsite
        to {path} {path}/ /anotherWebsite
      }
      redir {
        # /anotherWebsitewould usually still issue a 404
        # So manually redir from this path if it was rewritten to
        if {rewrite_uri} is /anotherWebsite
        if {path} is /
        if_op  or
        # Modify the destination and status as required
        / https://www.anotherWebsite.com
      }
    }