Relationship between rewrite, redir, respond and try_files?

Yeah. It’s not really magic though, it’s just the Location header in the response that tells HTTP clients “hey, look over there” :sweat_smile:

If you caddy adapt a Caddyfile with redir in it, you’ll see that it’s just a static_response with the Location header set:

:80

redir https://google.com

:point_down:

{
  "apps": {
    "http": {
      "servers": {
        "srv0": {
          "listen": [
            ":80"
          ],
          "routes": [
            {
              "handle": [
                {
                  "handler": "static_response",
                  "headers": {
                    "Location": [
                      "https://google.com"
                    ]
                  },
                  "status_code": 302
                }
              ]
            }
          ]
        }
      }
    }
  }
}

You could make your own little URL shortener by using the map directive in your Caddyfile:

example.com {

	map {path} {redirect-uri} {
        	/old-about-page /about
	        /google https://google.com
	}

	@hasRedir expression `{redirect-uri} != ""`
	redir @hasRedir {redirect-uri}

	respond "No redirect!"
}
2 Likes