3

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 server@myWebsite.com
  root /var/www/html/
  rewrite / /anotherWebsite
  rewrite /titi /toto.png
  redir /anotherWebsite https://www.anotherWebsite.com/
}
Artentica
  • 65
  • 1
  • 8

2 Answers2

3

In Caddy 2, this problem can be solved much more easily:

rewrite /titi /toto.png
route {
    file_server /toto.png
    redir https://anotherwebsite.com
}

We are landing these improvements in beta 13 next week.

Matt
  • 22,721
  • 17
  • 71
  • 112
1

I found this possibility:

www.myWebsite.com, myWebsite.com {
  tls server@myWebsite.com
  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
  }
}
Artentica
  • 65
  • 1
  • 8
  • 1
    Thanks for your answer. This is one of the use cases I've been mulling over as I improve the Caddyfile for v2. Think we can get this down to about 3 lines in Caddy 2. – Matt Jan 04 '20 at 00:19