2

When working with canonical URLs, does a trailing slash make any difference on a root URL?

I put the following canonical tag into my Rails site head:

<link rel="canonical" href="<%= url_for(:only_path => false) %>" />

...to ensure any parameterized URLs resolve to the base URL.

However, when I navigate to http://www.example.com, the canonical link shows up with a slash at the end:

<link rel="canonical" href="http://www.example.com/" />

I know trailing slashes DO matter when a path element is present in the URL, but thought they didn't matter on root URLs. However, I then ran into Matt Cutts presentation on canonical tags, where he clearly states that they are considered different URLs:

From http://www.mattcutts.com/blog/canonical-link-tag/ (See slide 3):

These URLs are all different:

  • www.example.com
  • example.com
  • www.example.com/
  • example.com/

Can anyone shed some light on what he means?

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Yarin
  • 173,523
  • 149
  • 402
  • 512
  • Related: http://stackoverflow.com/a/11372402/165673, http://stackoverflow.com/a/2581423/165673 – Yarin Nov 10 '13 at 14:03
  • I voted to close this question because it is not a programming question and it is off-topic on Stack Overflow. Non-programming questions about your website should be asked on [webmasters.se]. In this case the question has already been asked and answered there: [Canonical URL for a home page and trailing slashes](https://webmasters.stackexchange.com/questions/31796/canonical-url-for-a-home-page-and-trailing-slashes) – Stephen Ostermiller Sep 26 '22 at 19:51

1 Answers1

3

URLs which point to directory names (often with the expectation that a web server handler will return some sort of 'index'') without a trailing slash are actually invalid. Most web servers will automagically correct these requests with a redirect to the same URL with a trailing slash added.

So behind the scenes, your request for http://example.com results in a redirect from the web server to http://example.com/ which is why you're seeing the surprise trailing slash.

The short answer is that proper URI paths matter everywhere- root directory or not. For a deeper and more ranty answer, take a look at this page.

  • halfway agree with what you're saying. Technically, a root url must contain a trailing slash because it's pointing to the root, and paths cannot be empty. On the other hand, it's pointing to a specific resource (the homepage) and not a directory per se, so in that sense a trailing slash is illogical. As for whether it matters for crawlers, most evidence i've seen so far suggests that for root urls it doesn't. – Yarin Nov 10 '13 at 14:01