Artentica
Artentica

Reputation: 65

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/
}

Upvotes: 3

Views: 10154

Answers (2)

Matt
Matt

Reputation: 23729

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.

Upvotes: 3

Artentica
Artentica

Reputation: 65

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
  }
}

Upvotes: 1

Related Questions