URL Redirection

Hello,

Just want to say…Caddy is amazing!

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.

  1. Is this Caddyfile correct for the purposes of URL redirection?
  2. Is the gzip directive necessary if all the server does is provide URL redirection?
  3. Any recommendations to make this Caddyfile as clean as possible?
http://a {
	root /var/www
	gzip
	redir https://long-url.com
}
http://b {
	root /var/www
	gzip
	redir https://very-long-url.com
}
2 Likes

Glad you like using Caddy!

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.

1 Like

Hey Matt,

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?

http://a {
	root /var/www
	gzip
	redir https://long-url.com
}
http://b {
	root /var/www
	gzip
	redir https://very-long-url.com
}
http://c {
	root /var/www
	ipfilter / {
		rule allow
		ip 192.168.1.0/24
		redir https://extremely-long-url/1
	}
	ipfilter / {
		rule allow
		ip 192.168.2.0/24
		redir https://extremely-long-url/2
	}
}

Hi @mitch,

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.

https://caddyserver.com/docs/placeholders
https://caddyserver.com/docs/rewrite
https://caddyserver.com/docs/redir

1 Like

Thank you for the information! Everything seems to be working as intended now.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.