In: , , , ,
On: 2004 / 05 / 08 Viewed: 39803 times

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)

CODE:
  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.

Related posts

Metastuff

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. You can trackback this post from your own site

12 Blablas

  1. 1
    RaiL-FleX France »
    thought, on 08/May/04 at 8:31 pm # :

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

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

  2. 2
    Ozh France »
    replied, on 08/May/04 at 8:49 pm # :

    bah, baisé :P
    Well, fucked :P

  3. 3
    RaiL-FleX France »
    wrote, on 08/May/04 at 11:06 pm # :

    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. 4
    Max Thrane Denmark »
    thought, on 10/May/04 at 6:38 pm # :

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

  5. 5
    RaiL-FleX France »
    thought, on 10/May/04 at 7:02 pm # :

    Sure I'm :)

  6. 6
    piou France »
    commented, on 28/Jun/04 at 7:00 pm # :

    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. 7
    RaiL-FleX United States »
    said, on 02/Jul/05 at 10:55 pm # :

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

  8. 8
    josh Australia »
    said, on 21/Jun/07 at 3:23 am # :

    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. 9
    Ozh France »
    wrote, on 22/Jun/07 at 11:05 pm # :

    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. 10
    Problem With Trailing Slash For WordPres... United States »
    pingback on 12/Jul/07 at 10:45 am # :

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

  11. 11
    David Bell Great Britain (UK) »
    replied, on 12/Oct/07 at 11:29 am # :

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

    Merci

  12. 12
    What is Apache? Israel »
    wrote, on 23/Nov/07 at 11:04 pm # :

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

Leave a Reply

Comment Guidelines or Die

  • HTML: You can use these tags: <a href=""> <em> <i> <b> <strong> <blockquote>
  • Posting code: Post raw code (no <> &lt; etc) within appropriate tags : [php][/php], [css][/css], [html][/html], [js][/js], [sql][/sql], [xml][/xml], or generic [code][code]
  • Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar.
  • Spam: Various spam plugins on patrol. I'll put pins in a Voodoo doll if you spam me.
  • I will mark as Spam test comments, all comments with SEO names (ie "My Cool Online Shop" instead of "Joe") or containing forum-like signatures.

Read more ?

Close
E-mail It