the trailing slash for domains and directory links

Date: 09 Mar, 2009
Posted by: admin
In: hints & tips|internet, web design & development|life & family|Search Engines, SEO & SEM

Bookmark and Share

This is something I’ve been seeing a lot recently so I checked with the docs before claiming to know the canonical position.

Adding a trailing slash to domain addresses

If you’re linking to the example.com domain, what do you put in your link anchors href? You should put href=”http://www.example.com/” and not href=”http://www.example.com“. Why?

Apache said so

If you look for it in the Apache / httpd web server docs you’ll find the following:

A “trailing slash” redirect is issued when the server receives a request for a URL http://servername/foo/dirname where dirname is a directory. Directories require a trailing slash, so mod_dir issues a redirect to http://servername/foo/dirname/.

[Apache module mod_dir documentation]

and

Typically if a user requests a resource without a trailing slash, which points to a directory, mod_dir redirects him to the same resource, but with trailing slash for some good reasons:

  • The user is finally requesting the canonical URL of the resource
  • mod_autoindex works correctly. Since it doesn’t emit the path in the link, it would point to the wrong path.
  • DirectoryIndex will be evaluated only for directories requested with trailing slash.
  • Relative URL references inside html pages will work correctly.

[ibid]

The take away, in case you missed it is that the canonical URL, a directories default file (eg index.html, index.php) or for a domain name, has a trailing slash. Without it the Apache web server (which you probably have powering your website) causes a search and an internal redirect to be made to find and link to the actual resource.

So,I’m OK with IIS then?

IIS says so too

Microsofts knowledge base as of March 2009 has this as the latest advice on trailing slashes:

When a browser requests a URL such as http://www.servername.de/SubDir, the browser is redirected to http://www.servername.de/SubDir/. A trailing slash is included at the end of the URL.[IIS generates courtesy redirect when folder without trailing slash is requested]

Then what happens?

Internet Information Server (IIS) first treats SubDir as a file that it should give back to the browser. If this file cannot be found, IIS checks to see if there is a directory with this name. If a directory with this name exists, a courtesy redirect with a 302 “Object moved” response message is returned to the browser. This message also contains the information about the new location of the directory with the trailing slash. In turn, the browser starts a new GET request to the URL with the trailing slash.

Trailing slashes are canonical then?

Well in a word, yes. We now have a new tag parameter value courteousy of Google – rel=canonical – for use in link tags. This parameter value allows us to declare the canonical URL of a resource. So for any of these:

  • http://example.com
  • http://www.example.com
  • http://www.example.com/index.html

Then the canonical resource is in fact most likely to be http://www.example.com/ (though you could choose the domain rather than the www subdomain if you want). One example of a big name getting this wrong is SEOmoz:

SEOmoz on use of "canonical" URLs

SEOmoz on use of "canonical" URLs

It doesn’t matter really though, right!?

You can clearly see by clicking this link /canonical and this link /canonical/ that they refer to different things (/canonical/ is redirected to the server root as I’m not allowed to actually create that directory on my file system, YMMV) . Why create [a little] greater server load and wasted time when writing canonical links why not use the ultimate canonical that your web server will use and add that trailing slash.


But, but, but …

SEOmoz is interesting here because they choose, presumably with a .htaccess rewrite rule, to serve an address with no trailing slash. This may represent a file which has its extension stripped for display in the browser or (more likely) it represents the name of a directory from which an index.php page is served (though it could be index.html, index.asp, etc.).

Here’s a little more reading on this.

Example telnet session showing 301 redirect

Lastly, if you’re a bit geeky or still looking for some evidence that missing trailing slashes are evil: this is a quick telnet session to retrieve the whitepaper section from 37signals:

$ telnet 37signals.com 80
Trying 72.32.62.117…
Connected to 37signals.com.
Escape character is ‘^]’.
GET /whitepaper HTTP/1.1
HOST: 37signals.com

HTTP/1.1 301 Moved Permanently
Date: Mon, 09 Mar 2009 05:47:11 GMT
Server: Apache/2.2.4
Location: http://37signals.com/whitepaper/
Vary: Accept-Encoding
Content-Length: 240
Content-T???: text/html; charset=iso-8859-1
[...]
Connection closed by foreign host.

$ telnet 37signals.com 80
Trying 72.32.62.117…
Connected to 37signals.com.
Escape character is ‘^]’.
GET /whitepaper/ HTTP/1.1
HOST: 37signals.com

HTTP/1.1 200 OK
Date: Mon, 09 Mar 2009 05:47:56 GMT
Server: Apache/2.2.4
Accept-Ranges: bytes
X-Powered-By: PHP/5.2.4
Transfer-Encoding: chunked
Content-T???: text/html

197
<html>
[...]

You can clearly see the 301 redirect when http://37signals.com/whitepapers is requested and that it’s redirected to /whitepaper/ so it will return the default resource from that directory.

Note that in the above Content-Type has been replaced to get past my hosts mod_security settings.

Over to you

Do you have strong feelings about trailing slashes, use them, don’t use them, ignore the whole situation? Let us know by posting a comment.

Useful post? Go on, buy me a treat I promise I'll share!
— recommended donation £2 / $3 / €2.5

2 Responses to "the trailing slash for domains and directory links"

1 | Danny Dover

March 11th, 2009 at 7:58 pm

Thank you for writing and sharing this very helpful post. I didn’t realize that servers automatically 301 redirect when the incomplete version of a URL is added. On large sites this would add up quickly. One small note, I believe you are using an old version of my SEO cheat sheet. The current revision (which is 6 months old) has the backslash added. Although it is only fair to note, that I added in on a suggestion from a friend who suggested it was better style to add it. Maybe they knew the underlining reason, but I did not :-)

Best of luck and thanks for teaching me something today!

2 | admin

March 14th, 2009 at 12:41 am

Danny thanks for your comment – the image was taken from the adjacent SEOmoz link (above) which for me still shows this graphic. As you rightly say your v.09 from Sept ’08 includes the final backslash in the advice – apologies. Perhaps the post should be superseded to come in line with the alterations? I’m not sure.

You’ve taught me plenty, glad I could return the compliment in some small way.


About

Flapjacktastic is just a random collection of musings, hints&tips, notes, information ... a collection of stuff really that's overflowed from the brain of this husband, father, potter, business-man, geek ...

past posts