Choosing between www and non-www URLs - HTTP 编辑
A recurring question among website owners is whether to choose non-www or www URLs. This page provides some advice on what's best.
What are domain names?
In an HTTP URL, the first substring that follows the initial http://
or https://
is called the domain name. This domain name is hosted on a server where the document resides.
A server isn't necessarily a physical machine: several servers can reside on the same physical machine. Or, one server can be handled by several machines, cooperating to produce the answer or balancing the load of the requests between them. The key point is that semantically one domain name represents one single server.
So, do I have to choose one or the other for my web site?
- Yes, you need to choose one and stick with it. The choice of which one to have as your canonical location is yours, but if you choose one, stick with it. It will make your website appear more consistent to your users and to search engines. This includes always linking to the chosen domain (which shouldn't be hard if you're using relative URLs in your website) and always sharing links (by email/social networks, etc.) to the same domain.
- No, you can have two. What is important is that you are coherent and consistent with which one is the official domain. This official domain is called the canonical name. All your absolute links should use it. But even so, you can still have the other domain working: HTTP allows two techniques so that it is clear for your users, or search engines, which domain is the canonical one, while still allowing the non-canonical domain to work and provide the expected pages.
So, choose one of your domains as your canonical one! There are two techniques below to allow the non-canonical domain to work still.
Techniques for canonical URLs
There are different ways to choose which website is canonical.
Using HTTP 301 redirects
In this case, you need to configure the server receiving the HTTP requests (which is most likely the same for www and non-www URLs) to respond with an adequate HTTP 301
response to any request to the non-canonical domain. This will redirect the browser trying to access the non-canonical URLs to their canonical equivalent. For example, if you've chosen to use non-www URLs as the canonical type, you should redirect all www URLs to their equivalent URL without the www.
Example:
- A server receives a request for
http://www.example.org/whaddup
(when the canonical domain is example.org) - The server answers with a code
301
with the header
.Location
: http://example.org/whaddup - The client issues a request to the canonical domain:
http://example.org/whatddup
The HTML5 boilerplate project has an example on how to configure an Apache server to redirect one domain to the other.
Using <link rel="canonical">
It is possible to add a special HTML <link>
element to a page to indicate what the canonical address of a page is. This has no impact on the human reader of the page, but tells search engine crawlers where the page actually lives. This way, search engines don't index the same page several times, potentially leading to it being considered as duplicate content or spam, and even removing or lowering your page from the search engine result pages.
When adding such a tag, you serve the same content for both domains, telling search engines which URL is canonical. In the previous example, http://www.example.org/whaddup
would serve the same content as http://example.org/whaddup
, but with an additional <link>
element in the head:
<link href="http://example.org/whaddup" rel="canonical">
Unlike the previous case, browser history will consider non-www and www URLs as independent entries.
Make your page work for both
With these techniques, you can configure your server to respond correctly for both, the www-prefixed and the non-www-prefixed domains. It is good advice to do this since you can't predict which URL users will type in their browser's URL bar. It is a matter of choosing which type you want to use as your canonical location, then redirecting the other type to it.
Deciding the case
This is a very subjective topic it could be considered a bikeshedding issue. If you wish to read deeper, please see some of the many articles on the subject.
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论