When should a trailing slash be used in a URL? For example - should my URL look like /about-us/
or like /about-us
?
I am fully aware of the SEO-related issues - duplicate content and the canonical thing; I'm trying to figure out which one I should use in the context of serving pages correctly alone.
For example, my colleague is thinking that a trailing slash at the end means it's a "folder" - a "directory", so this is not a correct style. But I think that without a slash in the end - it's not quite correct either, because it almost looks like a folder, but it isn't and it's not a normal file either, but a filename without extension.
Is there a proper way of knowing which to use?
When you make your URL
/about-us/
(with the trailing slash), it's easy to start with a single fileindex.html
and then later expand it and add more files (e.g.our-CEO-john-doe.jpg
) or even build a hierarchy under it (e.g./about-us/company/
,/about-us/products/
, etc.) as needed, without changing the published URL. This gives you a great flexibility.That's not really a question of aesthetics, but indeed a technical difference. The directory thinking of it is totally correct and pretty much explaining everything. Let's work it out:
You are back in the stone age now or only serve static pages
You have a fixed directory structure on your web server and only static files like images, html and so on — no server side scripts or whatsoever.
A browser requests
/index.htm
, it exists and is delivered to the client. Later you have lots of - let's say - DVD movies reviewed and a html page for each of them in the/dvd/
directory. Now someone requests/dvd/adams_apples.htm
and it is delivered because it is there.At some day, someone just requests
/dvd/
- which is a directory and the server is trying to figure out what to deliver. Besides access restrictions and so on there are two possibilities: Show the user the directory content (I bet you already have seen this somewhere) or show a default file (in Apache it is:DirectoryIndex: sets the file that Apache will serve if a directory is requested.
)So far so good, this is the expected case. It already shows the difference in handling, so let's get into it:
At 5:34am you made a mistake uploading your files
(Which is by the way completely understandable.) So, you did something entirely wrong and instead of uploading
/dvd/the_big_lebowski.htm
you uploaded that file asdvd
(with no extension) to/
.Someone bookmarked your
/dvd/
directory listing (of course you didn't want to create and always update that niftyindex.htm
) and is visiting your web-site. Directory content is delivered - all fine.Someone heard of your list and is typing
/dvd
. And now it is screwed. Instead of your DVD directory listing the server finds a file with that name and is delivering your Big Lebowski file.So, you delete that file and tell the guy to reload the page. Your server looks for the
/dvd
file, but it is gone. Most servers will then notice that there is a directory with that name and tell the client that what it was looking for is indeed somewhere else. The response will most likely be be:Status Code:301 Moved Permanently
withLocation: http://[...]/dvd/
So, totally ignoring what you think about directories or files, the server only can handle such stuff and - unless told differently - decides for you about the meaning of "slash or not".
Finally after receiving this response, the client loads
/dvd/
and everything is fine.Is it fine? No.
"Just fine" is not good enough for you
You have some dynamic page where everything is passed to
/index.php
and gets processed. Everything worked quite good until now, but that entire thing starts to feel slower and you investigate.Soon, you'll notice that
/dvd/list
is doing exactly the same: Redirecting to/dvd/list/
which is then internally translated intoindex.php?controller=dvd&action=list
. One additional request - but even worse!customer/login
redirects tocustomer/login/
which in turn redirects to the HTTPS URL ofcustomer/login/
. You end up having tons of unnecessary HTTP redirects (= additional requests) that make the user experience slower.Most likely you have a default directory index here, too:
index.php?controller=dvd
with noaction
simply internally loadsindex.php?controller=dvd&action=list
.Summary:
If it ends with
/
it can never be a file. No server guessing.Slash or no slash are entirely different meanings. There is a technical/resource difference between "slash or no slash", and you should be aware of it and use it accordingly. Just because the server most likely loads
/dvd/index.htm
- or loads the correct script stuff - when you say/dvd
: It does it, but not because you made the right request. Which would have been/dvd/
.Omitting the slash even if you indeed mean the slashed version gives you an additional HTTP request penalty. Which is always bad (think of mobile latency) and has more weight than a "pretty URL" - especially since crawlers are not as dumb as SEOs believe or want you to believe ;)
Who says a file name needs an extension?? take a look on a *nix machine sometime...
I agree with your friend, no trailing slash.
It is not a question of preference.
/base
and/base/
have different semantics. In many cases, the difference is unimportant. But it is important when there are relative URLs.child
relative to/base/
is/base/child
.child
relative to/base
is (perhaps surprisingly)/child
.From an SEO perspective, choosing whether or not to include a trailing slash at the end of a URL is irrelevant. These days, it is common to see examples of both on the web. A site will not be penalized either way, nor will this choice affect your website's search engine ranking or other SEO considerations.
Just choose a URL naming convention you prefer, and include a canonical meta tag in the
<head>
section of each webpage.Search engines may consider a single webpage as two separate duplicate URLS when they encounter it with and without the trailing slash, ie
example.com/about-us/
andexample.com/about-us
.It is best practice to include a canonical meta tag on each page because you cannot control how other sites link to your URLs.
The canonical tag looks like this:
<link rel="canonical" href="https://example.com/about-us" />
. Using a canonical meta tag ensures that search engines only count each of your URLs once, regardless of whether other websites include a trailing slash when they link to your site.In my personal opinion trailing slashes are misused.
Basically the URL format came from the same UNIX format of files and folders, later on, on DOS systems, and finally, adapted for the web.
Source: Wikipedia: Uniform Resource Identifier
Another good source to read: Wikipedia: URI Scheme
Source: Wikipedia Uniform Resource Locator (URL)
Also:
Source: Google WebMaster Central Blog - To slash or not to slash
Finally:
A slash at the end of the URL makes the address look "pretty".
A URL without a slash at the end and without an extension looks somewhat "weird".
You will never name your CSS file (for example) http://www.sample.com/stylesheet/ would you?
BUT I'm being a proponent of web best practices regardless of the environment. It can be wonky and unclear, just as you said about the URL with no ext.