In: , , , ,
On: 2004 / 05 / 08 Viewed: 89062 times
Shorter URL for this post:

On badly configured Apaches (like on errr… my host :), URLs referecing directories and lacking a trailing slash (like /mydir instead of /mydir/) lead to an error : the webserver looks for a file named mydir instead of a directory.

After some googling and lots of testing, I've come up with a solution. This should even work on Apaches with virtual hosts enabled and bad ServerName directive.

Put this in .htaccess in your document root (given example is for my site of course, replace the server name with yours)

  1. RewriteEngine  on
  2. RewriteBase    /
  3. RewriteCond    %{REQUEST_FILENAME}  -d
  4. RewriteRule    ^(.+[^/])$ $1/  [R=301,L]

Of course, you need mod_rewrite and the ability to override these settings in your .htaccess file.

Shorter URL

Want to share or tweet this post? Please use this short URL:


This entry "Apache and the trailing slash problem" was posted on 08/05/2004 at 7:58 pm and is tagged with , , , ,
Watch this discussion : Comments RSS 2.0.

15 Blablas

  1. RaiL-FleX says:

    Mais si Apache ne supporte pas le mode rewrite ? :P

    And if Apache doesn't support rewrite mod ? :P

  2. Ozh says:

    bah, baisé :P
    Well, fucked :P

  3. RaiL-FleX says:

    De toutes façons, linux, Apache et PHP ça pue vivent windows server 2003, IIS et ASP …

    Anyway, Linux, Apache and PHP suck, Win Server 2k3, IIS and ASP are the best …


  4. Max Thrane says:

    IIS? on Winblows? You must be kidding me :p

  5. RaiL-FleX says:

    Sure I'm :)

  6. piou says:

    euh, perso je ne fait jamais confiance au serveur, c'est pourquoi j'écris en dur les paramètres du site sous forme de constantes dans un fichier config chargé à chaque page

    c plus propre, et puis il paraît que les htaccess ralentissent les serveurs

  7. RaiL-FleX says:

    I retract my previous comments about Windows being the best. Turns out it sucks cock as hard as I do.

  8. josh says:

    any tips on how to get this working in a subdirectory? i have wordpress installed on my root and your fix seemed to be working very well. then i installed another installation under /blog/ and when i enabled pretty permalinks, it looks like the .htaccess file that is created under the /blog/ dir by wordpress is overriding the functionality of the .htaccess in the root dir.

  9. Ozh says:

    josh » had the same problem with my previous host. The workaround is simple: add this .htaccess trick to /.htaccess and /blog/.htaccess (before what WP inserts in this file)
    Other subdirectories though (/download or whatever that's not WP) shouldn't need the extra .htaccess trick.

  10. […] have tried searching the web and found this (link1) while Dorkarl from Google Group also referred me to […]

  11. David Bell says:

    Thanks Ozh, that worked beautifully for me… Been struggling with the trailing slash for days!


  12. Why do this? Doesn't this overload Apache, as now it has to serve two requests? What is the advantage?

  13. Magnus says:

    I think this will hurt your SEO ranking, two URLs (with & without slash) pointing to the same contents. Duplicated contents, from the crawlers' point of view.

  14. Ozh says:

    Magnus » nope. There's a 301 redirect, which is a permanent redirect.

  15. Magnus says:

    Okay, good to know.

    Now, when you mention there's a 301, I noticed the RewriteRule line actually ends with "[R=301,L]", which I didn't notice until now, since the code is not very wide. :-)

    (I'm surprised you answered this quickly, the post being 3.5 years old :-))

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar and sign for a free account
Spam: Various spam plugins may be activated. I'll put pins in a Voodoo doll if you spam me.

Read more ?