2

My short research on trailing slashes duplicate On page issues, covers only the dir & dir/ cases.

But what about the /file.html & /file.html/. Are they acceptable both or one of them should be redirected?

3

You should always have 1 version of the page to prevent duplicate content being flagged.

People structure their URL's differently, some people never have trailing slashes, some always have trailing slashes and some only have trailing slashes on directories and non on files. I believe the last option is to do with a slight improvement to server speed (will need to look this up further)

Make sure you always use a canonical or a 301 redirect and be consistent.

  • "trailing slashes on directories ... a slight improvement to server speed" - normal Apache behaviour is for mod_dir to "fix" the URL and append a trailing slash on a physical directory if it is omitted. Apache does this with a 301 redirect. So, yes, linking to a directory without a trailing slash would indeed see a performance penalty, in the same way that linking to a URL that redirects would. – MrWhite Feb 26 '17 at 17:19
2

Regardless of what you are linking to (physical directory, file or something entirely virtual), the URL with and without a trailing slash are two different URLs, so ideally they should be canonicalised.

These are two different URLs and could potentially return different content:

  • example.com/<whatever>
  • example.com/<whatever>/

As GrapeSoda suggests, whether you use trailing slashes or not on your URLs is largely down to personal preference. However (on Apache) when referencing a physical directory then a trailing slash is required in order to correctly serve the directory index.

  • For a directory, a trailing slash is technically correct regardless of the web server. This is largely because in the beginning, there was the directory and the file (biblical) meaning that any reference made to the web server had to match the path from the web root on the file system exactly. Today that is not exactly always the case for everything, however, the rules still apply when you are referencing anything to be served from the file system or assumed to be served by the file system whether that is actually the case or not. File system rules are presumed historically. Cheers!! – closetnoc Feb 26 '17 at 19:38
0

Two Pages

Clearly, a slash or no slash define two different resources, except at the root level because the HTTP request can't access the root without a slash.

The HTTP request for the root of a website looks like this:

GET / HTTP/1.1
Host: www.example.com

It's just not technically possible to distinguish a version without a slash even if the URL syntax allows you to write it...

About a website hierarchy Google says:

When referring to the homepage, a trailing slash after the hostname is optional since it leads to the same content (https://example.com/ is the same as https://example.com). For the path and filename, a trailing slash would be seen as a different URL (signaling either a file or a directory), for example, https://example.com/fish is not the same as https://example.com/fish/.

Notice that here the author says "signaling either a file or a directory". Another hint of the thought about slashes at Google.

Filename, most commonly no slash when there is an extension

Since file.html is referencing a file, it generally should not have a slash at the end (i.e. on a hard drive, you don't put slashes at the end of filenames.) However, on the web you see it all and both appear here and there. I still think no slash is more common when you have an extension such as the .html.

I've seen CMS that place files attached to a page in a virtual sub-folder under that very page. So for example, if you had a recipe.pdf file, it could be:

http://www.example.com/path/file.html/recipe.pdf
  or
http://www.example.com/path/file.html/recipe.pdf/

Personally, I think it is weird to have a sub-directory under a .html because you can't replicate that in a file system (in case you wanted later to create a static version of your website... you'd be in trouble with such virtual folders.)

A Google Example

Google had some examples about this problem (I'll add the link if I find that page...) Their examples removed the extension as in:

http://www.example.com/path/file/recipe.pdf

(Note: Google says that they use the extension as a hint of the page's contents, so file.html tells them that the content is most certainly HTML.)

So no slash at the end of what represents files, but when no extension is present, we get a folder and it makes sense to have a slash. So a complete list of the files could look like this:

http://www.example.com/path/file.html
http://www.example.com/path/file/
http://www.example.com/path/file/recipe.pdf

Note: In the past, Google said that they would test parent folders automatically, even if the parent page is not defined in the sitemap.xml or in a link. So GoogleBot would check out all of the following:

# Found document:
http://www.example.com/path/file/recipe.pdf

# Also check parents:
http://www.example.com/path/file
http://www.example.com/path
http://www.example.com/

However, I don't recall ever seeing such hits in my logs. I never really look to prove that statement either. Yet, this means a functional parent is a good idea. Actually, as a user, I do that once in while, because when I see I'm in a certain sub-folder and think that I should be able to find things of interest one directory up, I manually try to go there by changing the URL. Website that break when you do that are definitely annoying.

Browser's Saved Page

When you save a webpage from Firefox or other browser, they create a sub-directory for all the files attached to the page (CSS, JS, images, etc.) and name it with the extension but change the period with an underscore. So something like this:

http://www.example.com/path/file_html/recipe.pdf

Redirection & Canonical

As for redirecting, it is not mandatory if your sitemap.xml and canonical are correct. In other words, if the search engines goes to:

http://www.example.com/path/file.html/

but finds the following link meta tag:

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

then no 301 is required. Google & Co. will use the version without the slash.

The opposite is true too, of course. With the following, the slash version is saved and still no 301 is required:

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

If you don't have a proper canonical (or as I've seen on some CMS, if the code generating the canonical copies the URI as is--so you get two different pages in this case,) a redirect is probably the easiest way to fix the problem.

The sitemap.xml file must reference the page using the canonical URL of the page. So if the canonical uses the slash, the sitemap.xml URL will include the slash, and vice versa.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.