∅ the empty set

Watch that trailing slash

Forward slash

The trailing slash on URLs referencing directories has always bugged me because it doesn't look good. I am a bit of a URL fetishist, I like URLs to be simple and clear and without a trailing slash. But that is not the correct way of doing things.

Back in the early days, a website was essentially a collection of directories and index.html files in them with a number of of other .html files around (who remembers the difference between home.html and index.html?). The directory's name was appeared plainly in the URL and that wasn't a problem as long as it was followed by a trailing slash (or a filename).

If they are missing, the server coughs, because if you say /somedir/foo instead of /somedir/foo/ the server searches for a file named foo, and because this file is a directory, it complains and tries to fix it by itself. A List Apart has a good article on trailing slashes.

Notwithstanding that it generate an extra disk access and slows the whole process down for your user, it usually works out. Except in a few cases. Typically if the last portion of the URL is actually a directory on the server's filesystem and you're rewriting the URL to remove the trailing slash for cosmetic reasons. Like /weblog where 'weblog' is an actual directory but is rewritten into /blo_index.php?div=blo&sec=hom.

RewriteEngine on
^weblog/$ /weblog [R]
^weblog$ /sub_index.php?div=blo&sec=hom

Why would you need to do this you might be wondering? Well, in this particular case there is a CMS that drives the weblog of a specific site, and generates content include files in the 'weblog' directory which are displayed by a PHP script located at the root of the site (or in any other directory for that matter). The same script displays different sections of the site, including the weblog's main page.

Now, initially, I wanted the URL without a trailing slash, and used a rewrite rule that mapped it onto the PHP script. Unfortunately, that doesn't work. You'll get /weblog/?div=blo&sec=hom show up in your browser if you omit the trailing slash in the URL.

The correct rewrite is to add the slash if missing:

RewriteEngine on
^weblog$ /weblog/ [R]
^weblog/$ /sub_index.php?div=blo&sec=hom

Use trailing slashes for directories because that is the correct way of doing things.

A direct consequence for me of all this was to have directory and file names that don't appear in the URL per se. This approach turned out to have several advantages:

  1. The URL doesn't give up anything about how the file system organisation (security);
  2. The URLs can be permanent in time and independant from the server's file system and technology (expandability);
  3. Forces you to use a directory/file naming scheme that prevents accidental overwriting of files.

Who hasn't at least once accidentally overwritten the wrong file while uploading an amended copy with the same name (e.g. index.html) to the server?

I like to organise the sections of a website in directories with each their own 3 letter code: sec_pro for products section, sec_con for contacts section, and so on. In each directory, the index file will prepend the 3 letter code: pro_index.html, con_index.html, etc. This way you ensure that there aren't two files on the server that bear the same name.

This scheme can be pushed further to subsection organisation: con_sales.html, con_support.html, con_corporate.html.

The content is in a separate file: con_sales_content_intro_inc.html, con_sales_content_main_inc.html, con_sales_content_extra_inc.html, etc. and located in its own separate directory: con_content. The file system looks like this:


This might seem a little overkill at first, but reveals to be extremely flexible and efficient in the long run. The content files can be CMS driven or not, depending of the context, users and level of expertise.

Call me a file system maniac if you like, but keeping to a neat, concise, and structured directory layout benefits everyone, and ensures that your site will not break when management decides to install the latest content management system developed in yet another emerging web–based development environment.

Ø permalink: https://davidroessli.com/logs/2007/06/watch_that_trailing_slash/

Reponses to “Watch that trailing slash”

#1 by andy

10:22 on 11 February 2008

Trailing Slashes are good in way. Its better to use it when you are submitting your website or making a text link. Here is a good blog related to these issues, that u find worth to read

#2 by Java Tutor

10:58 on 12 September 2008

Really nice tutorial. Really love your blog and i'm gonna subscribe to the RSS feed.

#3 by health insurance

01:19 on 29 December 2009

I am lost, I noticed google webmaster tools has me indexed with the trailing slash half of the time and because I am using wordpress on a windows server I am having a hard time fixing the issue. So I am trying to figure out if I should (for SEO purposes) use the slash even though none of my backlinks point that way.
Any help on this would be much appreciated.

#4 by buy essays

15:33 on 18 May 2011

Thanks for the article.

#5 by seo services

07:30 on 30 August 2011

This is a really excellent read for me. Must agree that you are one of the best bloggers I ever saw. Thanks for posting this useful article

#6 by assignment help

04:05 on 14 September 2011

Thank you,this is just what i needed.I have a presentation that I am just now working on, and I have been trying to find such information. Coconut Oil Plant.

#7 by Obat Nyeri Sendi

10:29 on 4 August 2013

Thanks a lot for sharing us that very useful information, I am very happy to have this

#8 by buy essays

13:12 on 6 January 2015

Thanks for this useful information..
P.S.buy essays

#9 by anny

12:25 on 9 April 2015

I totally agree with you. Also and i like URLs to be simple and clear and without a trailing slash. I hear so many useful things from you here. Thanks a lot! Please keep it good posting! If you need a help with your blog - www.custom-paper-writing.org. can write all types of texts for you!

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Previous: Embracing iTunes Plus, or from LPs to MP4a, or some 32 years of music

Next: Bear with me


Hello, my name is David Roessli. I am a freelance web designer and developer based in Geneva, Switzerland.

This weblog is an nth attempt to solve my multiple online personalities and weblog/rss feeds burnout issues. (more)


I have been contemplating the idea of upgrading my desktop Mac since this spring. The latest 27" iMac (Quad-Core) seemed the perfect candidate, but the release of Apple's 27" Monitor last September made me stick with the Mac Pro...


The autopsy of an iconic album cover picked up on Kottke.org. A stacked graph of successive radio signals from pulsar CP 1919, in a 1977 astronomy encyclopedia that originated in a 1970 Ph.D. thesis. Fascinating <3...


Check out my latest Flickr ramblings. Mostly day to day cameraphone pictures stolen here and there.

© 2000-2016 David Roessli | v4.1 | as valid xhtml and css as possible | hosted by pair Networks | RSS feeds.