Score:-1

Are sites normally hosted on www.example.com and example.com?

eg flag

I don't like the idea of hosting on both example.com and www.example.com

but I can't get my dns setup to redirect www.example.com to example.com. So what I have right now is both of them work and serve up the webpage. If I have:

A     example.com     123.4.5.6
CNAME www.example.com example.com

shouldn't that do a redirect? I realize I can do this fairly easy with JavaScript.

I just don't like the idea of putting all of my dev/prod focus on example.com while having www.example.com out there that some users could be going to and I won't be keeping a close enough eye on it to know if it has problems. For example, say I accidentally hard code some javascript to look for to "http://example.com". Well, that won't be there on the "http://www.example.com" version of the site.

What is the right thing to do here? Can DNS do the redirect for me? Should I redirect www.example.com to example.com with Javascript? ...JavaScript won't cover API calls though.

Score:6
cn flag

Generally it's considered best practice for a web site to only actually serve its content under one domain name, and have any other alias names only redirect to that one canonical domain name.

However, that redirect is an HTTP concept, not a DNS one. A CNAME record does not redirect anything, it just makes it so that one name resolves the same as another name in DNS.

The normal approach would be to have a web server facilitate the redirect directly in the HTTP response headers (through web server configuration, or done by the application handling the request if applicable); by sending HTTP status 301 (or in some contexts 302) with a Location header.

As for other variations on achieving redirection in a browser, one could serve HTML with meta refresh or possibly even Javascript as you suggested. That will however run the risk that some less browser-like HTTP clients will not follow the redirect.

Also, unless it serves a purpose, I would suggest that it's probably better to not actually have both these names even point to the same site in the first place, but instead have a separate site dedicated to redirect handling to avoid any confusion.

As for API clients, that one more or less takes doing things right in the first place to avoid having people start using the different names at all, because if they do you may not be able to fix it on your end without outright breaking those clients. Ie, document which URL should be used, and either have no API available at all under the other name(s) or have those redirect just to make it clear where the API actually is. Just don't expect the API clients to actually follow redirects, because they likely won't.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.