I’m running an Ubuntu 16.04 LTS 64-bit virtual machine with Caddy 0.10.3. The sole purpose of this machine is to provide URL redirection for intranet users, which it seems to do flawlessly. The machine does not serve any files of its own. With my sanitized Caddyfile below, a user can enter “a/” or “b/” in their browser and it will redirect them to https://long-url.com or https://very-long-url.com, respectively.
Is this Caddyfile correct for the purposes of URL redirection?
Is the gzip directive necessary if all the server does is provide URL redirection?
Any recommendations to make this Caddyfile as clean as possible?
Strictly speaking, root is not necessary when you have a catch-all redirect, however it’s not a bad idea to jail all sites like this to some folder that is expected to be public.
gzip is not needed.
Otherwise looks good! Normally I discourage serving sites on http:// like that but you are serving internal sites that can’t be validated by a public CA, so I see why you did that.
Thanks for the clarification! Now onto a bit more difficult question…
Say I wanted to add a third label (I think label is the correct term?) to my Caddyfile. This new label, “c/” would be utilized by all intranet users, but should redirect them to different webpages based on their IP address. Should this work and is it the “correct” syntax to use?
This wouldn’t work because ipfilter has no redir subdirective. You’ll need to get slightly creative to achieve redirects based on remote IP.
One of the placeholders available in the Caddyfile is the {remote} placeholder, which will let you do comparisons against the client’s IP.
You’ll need to rewrite the request based on that placeholder. Then you’ll need to specify a redirect for the request you’ve rewritten to. It will look something like this for the c label:
http://c {
root /var/www
# First redirect
rewrite {
if {remote} starts_with 192.168.1.
to /long-url-1
}
redir /long-url-1 https://extremely-long-url/1
# Second redirect
rewrite {
if {remote} starts_with 192.168.2.
to /long-url-2
}
redir /long-url-2 https://extremely-long-url/2
}
Keep in mind that for the other labels (a and b) in your example, you can skip the root and gzip directives as you have catch-all redirects preventing them from ever being relevant.