Domain IP address for www and non-www for Canonical URL
Asked Answered
P

3

55

To handle Canonical URL is it best practice to do a 301 redirect or better to have the same IP Address for both www and non www domain?

For example:

Canonical URL/domain wanted is http://example.com

Domain           | A Record
------------------------------------
example.com      | 192.0.2.34
www.example.com  | 192.0.2.34

I understand people may have a CNAME record for www (alias, given that CNAME is called Canonical Name); unsure if this is best practice compared to using the same IP address.

Or is this better.

Domain           | A Record
------------------------------------
example.com     | 192.0.2.34
*  www.example.com 301 redirect to example.com
Phosphatize answered 24/10, 2013 at 13:8 Comment(0)
P
136

The mechanisms you describe (A and CNAME records vs. 301 redirects) are part of two different protocols (DNS and HTTP). A and CNAME records have nothing to do with which site your HTTP server serves for different requests.

Let's look at two different DNS configurations:

Configuration 1 (CNAME record)

Host             | Type  | Data
-----------------+-------+-------------
example.com      | A     | 192.0.2.34
www.example.com  | CNAME | example.com
  • nslookup example.com resolves to 192.0.2.34
  • nslookup www.example.com resolves to 192.0.2.34

Configuration 2 (A records)

Host             | Type  | Data
-----------------+-------+-------------
example.com      | A     | 192.0.2.34
www.example.com  | A     | 192.0.2.34
  • nslookup example.com resolves to 192.0.2.34
  • nslookup www.example.com resolves to 192.0.2.34

In both cases your canonical domain and your www subdomain resolve to 192.0.2.34. However, the only thing your HTTP server will recognize is that it receives requests for both example.com and www.example.com on the same IP address. But it doesn't know whether you used A or CNAME records for that.

TL;DR

You have to use HTTP 301 redirects to enforce the canonical example.com in HTTP requests. But that has nothing to do with your DNS configuration.

Penneypenni answered 25/10, 2013 at 14:36 Comment(11)
Thanks for the detailed explanation. HTTP redirect or an A record to the server that will complete the redirect will sort out canonical url issues. You are a kind person, I appreciate your time.Phosphatize
Nice explanation. But I have one question. how to force to use www instead canonical? For one of my domain it serves only canonical domain even though I visit www. it redirects to canonical url. Any idea?Marqueritemarques
@SD The redirects are caused by your webserver. Check your webserver configuration whether there are any redirects from the www domain to your canonical domain and remove them if you don't want that.Penneypenni
@Penneypenni do you mean I should check apache config for this?Marqueritemarques
@SD yes, definitelyPenneypenni
@Penneypenni I just checked apache.conf, conf-enabled/* and also sites-enabled/000-default.conf file. I didn't find anything suspicious. That may be because I wasn't sure what I was looking for. Will you please help me here?Marqueritemarques
@Penneypenni I am done. All settings were correct but the url coming from db. Silly me. Anyway, thanks for you help here.Marqueritemarques
wrong, it will still show as www.mydomain.com. I don't get how is that an accepted answer. The only solution here is by ChetabahanaBowling
@Bowling care to elaborate on that? From my understanding, Chetabahana's answer solves the problem by having clients send their first HTTP request for www.mydomain.com to redirect.center's servers, which then do an HTTP 301 redirect to mydomain.com. So you'd have to rely on someone else's infrastructure to achieve something that you could easily configure yourself.Penneypenni
@Penneypenni there is no way to redirect using CNAME. If the OP wants Canonical URL/domain wanted is http://mydomain.cоm then there is only one way to do a redirect. Otherwise you will end up displaying website on http://mydomain.cоm and http://www.mydomain.cоmBowling
@Bowling that's basically what I am saying, isn't it?Penneypenni
B
5

You may try redirect.center. Its redirects domains using DNS only.
So it will fulfill your DNS configurations:

Domain           | A Record
------------------------------------
example.com      | 192.0.2.34
*  www.example.com 301 redirect to example.com

set your DNS record to:

CNAME | www | example.com.opts-https.redirect.center

Note:
The .opts-https. parameter redirect to https domain
If you use only http domain then use .opts-uri.

Bourbon answered 21/2, 2018 at 18:2 Comment(1)
this is a solution i was looking for all my life :) thanks!!Bowling
R
1

In DNS the canonical name record is pointing to the "one true DNS entry". The end result is that DNSName1 has the same IP address as DNSName2.

Comprehension is complicated by the fact that the CNAME is not the canonical name. The CName Record points to the canonical name. (The canonical name is the A record).

Canonical link: For search engines the canonical link in the html header of a page specifies the "one true page". You can get to multiple pages but you use the canonical link to tell the search engine that this page is the same as the other page. Doing this makes the search engine happy that you are not gaming the system by pretending to provide more content where it is only duplication. It only indexes it once.

301 (http) redirects: For search engines this says that page x is now replaced by page y and all the search ranking can be transferred to page y. If it is a "cosmetic" domain-name-standardisation it also tells the search engine to only index one page and is again a way to show that you're not trying to trick the search engine.

Ricky answered 27/3, 2017 at 6:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.