Www handling - Use the same multiple site definition or use redirection?

A general query…

I’m not sure I understand the difference between the two structures below. Is the end result the same for both, or, is there something subtle happening here that I’m not appreciating?

www.domain.com domain.com {
  ...
}

and

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

domain.com {
  ...
}

The first will actually serve the content on www.domain.com as well, whereas the second will always redirect www.domain.com to domain.com.

1 Like

Okay. I guess I’m not fully understanding the difference between redirect and serve in this context. For the casual user browsing www.domain.com, I assume the effect will be the same. They are none the wiser to what they’re seeing returned in the browser window.

To help me develop a deeper understanding, a follow-up question please… ‘In what context is one approach preferred over the other?’

The difference is that when the content is served on www, the browser will still show www in the address bar. With a redirect, the first request will tell the browser “no, go over there” and then the content will be served, but without www in the address bar.

Generally it’s better to only have one singular domain from which content is served from for SEO and such, I think, cause otherwise it can look like duplicate content from two different domains. But I’m no SEO expert so take that with a grain of salt.

1 Like

So, for those of us in the international community who have to deal with country suffixes, is this structure…

www.domain.com, domain.com,
www.domain.com.au, domain.com.au {
   ...
}

…better served by this structure?

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

domain.com.au {
  ...
}

Depends what you want, but sure :+1:

1 Like

If I use redirection for that last example, would I still end up with four certificates or just one?

Still four. Caddy doesn’t have issued multi-SAN certificates, and you didn’t configure it to use a wildcard.

1 Like

Based on as always helpful advice I received from @francislavoie in this thread, I’ve begun to move away from multisite definition and towards redirection. A noob question about {uri}…

An extract from the redir documentation, specifically the examples.

Redirect all requests to https://example.com:

redir https://example.com

Same, but preserve the existing URI:

redir https://example.com{uri}

Same, but permanent:

redir https://example.com{uri} permanent

I try leaving out {uri} and the redirection still works, so, by including it, what does ‘preserve the existing URI’ actually achieve? If I add in the permanent switch, how does that change again? I’d like to understand these subtleties please.

Basically having {uri} will make a request to http://www.example.com/some/path redirect to https://example.com/some/path, whereas if you remove {uri} it would redirect to https://example.com, i.e. lose the path portion. It’s nicer for users if they clicked a link from somewhere else to actually go to the page they wanted to go to when redirecting, instead of throwing them back to the root of the site.

Adding permanent changes the HTTP status code used in the redirect response. As noted in the docs, the default is “temporary”, i.e. 302. The difference is that a 301/permanent redirect will be remembered by the browser to avoid the extra roundtrip, and also any bots/crawlers might use that to determine that forget the old URL and only keep the final destination. Just google “difference between 301 and 302” and you’ll find plenty of explanations.

1 Like

I had no idea. I’ve learnt something new.

I’ve shied away from SEO, but it’s beginning to dawn on me a lot of the tuning that can be done in Caddy helps with SEO. This is previously uncharted territory for me.

Honestly, for me too. I’ve never actually put effort into sites with the aim of getting indexed, I’m primarily a backend dev for SaaS-type apps.

Okay, this is getting weird…

I start the conversion of domains to use redirection…

www.xenografix.com.au {
  redir https://xenografix.com.au{uri} permanent
}

xenografix.com.au {
  ...
}

No problem. That worked! Working through the various domains, everything is fine, until…

www.readymcgetty.com.au {
  redir https://readmcgetty.com.au{uri} permanent
}

readymcgetty.com.au {
  ...
}

This time, I message about too many redirects…
urlx

However, if I switch things around to redirect to www.readymcgetty.com.au instead,

readymcgetty.com.au {
  redir https://www.readmcgetty.com.au{uri} permanent
}

www.readymcgetty.com.au {
  ...
}

…it all works again. What’s going on here?

This seems to align to something I’ve seen in the documentation under Common Caddyfile Patterns, which I’ve never really understood. Extract below…

To add the www. subdomain with an HTTP redirect:

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

www.example.com {
}
To remove it:

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

example.com {
}

If you had the redirect marked the other way around and the browser cached it, then it might be causing you to run in a loop. Or maybe the site you’re serving is doing its own redirect.

You can run curl -v to see what the response to the redirect looks like – see the Location: header to see what the value is.

Not enough info to go on from what you wrote so far to know exactly what the problem is.

You were spot on with your assessment here. :+1:

Caddy and the support on this forum is just the duck’s nuts! :ok_hand:

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.