Apache and the trailing slash problem

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    ^(.+[^/])$           http://frenchfragfactory.net/$1/  [R=301,L]

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

15 comments

  1. RaiL-FleX

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

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

  2. Ozh

    bah, baisé :P
    Well, fucked :P

  3. RaiL-FleX

    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 …

    :-X

  4. Max Thrane

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

  5. RaiL-FleX

    Sure I'm :)

  6. piou

    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

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

  8. josh

    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

    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. Problem With Trailing Slash For WordPress in Godaddy | Charles Lau dot Com

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

  11. David Bell

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

    Merci

  12. What is Apache?

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

  13. Magnus

    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

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

  15. Magnus

    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 :-))

Comments are closed.