Caddy: How to handle subdomains?

Created on 22 Jan 2020  路  2Comments  路  Source: caddyserver/caddy

I'm running Caddy 2 and could not find any documentation on multiple subdomains.

Basically I want to redirect www to non-www. This is due to some people who always type www before a domain which causes a broken page.

Here's my caddyfile:

montanaflynn.com
root * /var/www/html
file_server

Thanks, excited to start using caddy but it seems like all the docs / issues I found were outdated.

question

Most helpful comment

It would look like this:

montanaflynn.com {
    root * /var/www/html
    file_server
}

www.montanaflynn.com {
   redir * https://{http.request.host.labels.1}.{http.request.host.labels.0}{path}
}

Here we're using request placeholders for the redirect, so that if you have multiple sites, you can just add them to the site block, for example www.montanaflynn.com, www.example.com {.

You could just hard-code the redirect destination if you prefer, i.e.

redir * https://montanaflynn.com{path}

All 2 comments

It would look like this:

montanaflynn.com {
    root * /var/www/html
    file_server
}

www.montanaflynn.com {
   redir * https://{http.request.host.labels.1}.{http.request.host.labels.0}{path}
}

Here we're using request placeholders for the redirect, so that if you have multiple sites, you can just add them to the site block, for example www.montanaflynn.com, www.example.com {.

You could just hard-code the redirect destination if you prefer, i.e.

redir * https://montanaflynn.com{path}

Thanks @francislavoie ! And you can actually omit the wildcard matcher (*) in these cases, since the next argument does not look like a path matcher (does not start with /). It is a matter of preference.

The new docs site will be live in a few hours which will have the new Caddyfile docs, check them out @montanaflynn !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

treviser picture treviser  路  3Comments

mschneider82 picture mschneider82  路  3Comments

jgsqware picture jgsqware  路  3Comments

PhilmacFLy picture PhilmacFLy  路  3Comments

lorddaedra picture lorddaedra  路  3Comments