Domain IP address for www and non-www for Canonical URL

后端 未结 3 1539
无人及你
无人及你 2021-01-30 08:43

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 UR

3条回答
  •  爱一瞬间的悲伤
    2021-01-30 08:56

    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.

提交回复
热议问题