How to redirect http and https to different sites(or roots) for one domain?


(Candura Tsang) #1

For example:

http://www.abc.com -> /var/www/siteone
https://www.abc.com -> /var/www/sitetwo

I tried to write Caddyfile like this:

www.abc.com:80 {
  root /var/www/siteone
  gzip
}

www.abc.com:443 {
  root /var/www/sitetwo
  gzip
}

And the result is

404 Site www.abc.com is not served on this interface

Then I tried to write Caddyfile like this:

http://www.abc.com {
  root /var/www/siteone
  gzip
}

https://www.abc.com {
  root /var/www/sitetwo
  gzip
}

The result is that http and https both enter sitetwo.

So, How to redirect http and https to different sites(or roots) for one domain?


(Matthew Fay) #2

Both of those options should be working. I did some testing below.

whitestrake at apollo in ~/Projects/test
❯ caddy -version
Caddy 0.11.1 (non-commercial use only)

whitestrake at apollo in ~/Projects/test
❯ mkdir one two

whitestrake at apollo in ~/Projects/test
❯ echo "Test one" > one/index.txt

whitestrake at apollo in ~/Projects/test
❯ echo "Test two" > two/index.txt

whitestrake at apollo in ~/Projects/test
❯ cat Caddyfile
http://localhost:8080 {
  root /Users/whitestrake/Projects/test/one
}

https://localhost:8443 {
  tls self_signed
  root /Users/whitestrake/Projects/test/two
}

whitestrake at apollo in ~/Projects/test
❯ caddy
Activating privacy features... done.
http://localhost:8080
https://localhost:8443

whitestrake at apollo in ~/Projects/test
❯ curl localhost:8080
Test one

whitestrake at apollo in ~/Projects/test
❯ curl -k https://localhost:8443
Test two

I note that www.abc.com is currently pointed at an Apache server, which issues redirects to Varnish/Amazon S3 at abc.go.com. You’ll need to point it at your Caddy server instead.


(Candura Tsang) #3

Thank you very much for your reply.

I think I found the cause of the problem.
My Chrome automatically redirects from http to https.
I changed to another device and it works fine. :slight_smile:


(Magikstm) #4

If you want to change the behavior for a specific domain in Chrome, this may help:


(Candura Tsang) #5

Oh, It’s very helpful~
Thank you~