Redirect from www.domain.com to domain.com

Hi all.

Newbie to Caddy here, just kicking the tyres [I currently use Nginx on all my sites]. Could someone show me the recommended Caddy way for redirecting from www.domain.com to domain.com. I can’t find any examples of this in the docs.

Cheers.

Hi, welcome. :slight_smile: Try adding this to your Caddyfile:

www.domain.com {
    redir https://domain.com{uri}
}

OK. Cancel that drivel underneath. It’s sorted now.

I was using Opera before. I’ve just tried in Chrome and it works perfectly. I don’t know WTF is going on in Opera’s tiny mind, with all that lunatic recursive redirecting.

Cheers for your help!


DELETED

1 Like

Maybe Opera was doing some aggressive caching or something…

Must have been something like that. Don’t know if you saw my original post before I deleted it, but navigating to either www.domain.com or domain.com, Opera was trying to load:

https://www.domain.com/domain.com/domain.com/domain.com/domain.com/domain.com/domain.com/domain.com/domain.com/domain.com/domain.com/domain.com/domain.com/domain.com/domain...

before giving up with a “too many redirects” error.

leading me to erroneously assume that the redir bit of my Caddy config was at fault.

1 Like

I’ve actually been tearing my hair out over this today.
these redirects are driving me crazy!!!

https://www.domain.com/domain.com/domain.com/domain.com/domain.com/domain.com/domain.com/domain.com/domain.com/domain.com/domain.com/domain.com/domain.com/domain.com/domain

My site has been running on caddy for months and today started acting weird in a number of browsers; the issue was reported to me by one of the site users first. Out of the blue, I didn’t change anything and now I’ve been fighting it all day.

I do the opposite of you, domain.com to www.domain.com & I’m frustrated to the point of not being able to think straight; any help is appreciated.

browsing to https://www.domain.com works, http doesn’t redirect to https though.

My caddyfile is below, caddy version is 0.9.3 (it was 0.8.* earlier today, upgraded hoping that would fix things)

example.com {
tls name@example.com
redir https://www.example.com{uri}
}

www.example.com {
root /var/www/wordpress
tls name@example.com
log /var/log/caddy/access.log
errors /var/log/caddy/error.log
gzip

fastcgi / /var/run/php/wordpress.sock php

# Routing for WordPress
rewrite {
    if {path} not_match ^\/wp-admin
    to {path} {path}/ /index.php?{query}
}

}

I was only seeing that recursive redirection in Opera. All other browsers were behaving as expected. I “solved” [for tenuous definitions of “solved”] the problem by clearing Opera’s caches.

However, I was in the process of actually tinkering with my Caddyfile at the time, so that kinda makes things breaking to be expected. It seems even more puzzling that you suddenly started seeing this happen, without having touched anything in your setup.

Over to you, Matt!

I’m pretty swamped with research for the next few days; if anyone would like to take a look in the code in the meantime, redirect.go would be the file to start in.

I’m also new to Caddy and am just setting it up and ran into the same issue which I resolved follows:

# Setup vhost for domain.com and www.domain.com
domain.com, www.domain.com {
    root /home/domain/public_html/
}

You also need to setup a Domain Pointer for www.domain.com I use Digital Ocean and this is a matter of adding the www entry using Networking | Domains. Otherwise when you start caddy you will get the error:

Activating privacy features...2016/12/14 23:51:58 [www.domain.com] failed to get certificate: acme: Error 400 - urn:acme:error:connection - DNS problem: NXDOMAIN looking up A for www.domain.com

You can check that the Domain Pointer is set as follows:

$ dig +short A domain_name

which will display the server’s IP address. You need to do this for www.domain.com and domain.com

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