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)
- RewriteEngine on
- RewriteBase /
- RewriteCond %{REQUEST_FILENAME} -d
- 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.
Shorter URL
Want to share or tweet this post? Please use this short URL: http://ozh.in/z
Mais si Apache ne supporte pas le mode rewrite ? :P
And if Apache doesn't support rewrite mod ? :P
bah, baisé :P
Well, fucked :P
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
IIS? on Winblows? You must be kidding me :p
Sure I'm :)
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
I retract my previous comments about Windows being the best. Turns out it sucks cock as hard as I do.
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.
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.
[…] have tried searching the web and found this (link1) while Dorkarl from Google Group also referred me to […]
Thanks Ozh, that worked beautifully for me… Been struggling with the trailing slash for days!
Merci
Why do this? Doesn't this overload Apache, as now it has to serve two requests? What is the advantage?
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.
Magnus » nope. There's a 301 redirect, which is a permanent redirect.
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 :-))