On a recent project I’ve got the end users building pages using Textile
and creating links like “My Link”:local_page, which when followed
results in a URL with no trailing slash, e.g.
example.com/dir/local_page
This is okay until there’s a relative link from local_page to sub_page
which yields a URL of example.com/dir/sub_page instead of
example.com/dir/local_page/sub_page. I could try to train the users to
put trailing slashes in their links, but that kind of approach always
breaks. I want to rewrite any URLs that don’t end with a filename
extension to add the trailing slash.
I found a similar problem referenced in the mailing list at
http://lists.radiantcms.org/pipermail/radiant/2006-September/001859.html
which induced me to read a little on mod_rewrite.
Here is what I wound up doing in my .htaccess file. I’d love to know if
anyone knows a better way of fixing this.
My regex matches when none of the following conditions are met:
URI ends with filename extension: .[^/]*$
URI ends with a slash: [^/]/+$
URI is the top level: ^/$
URI is in the admin tree: ^/admin.*
RewriteCond %{REQUEST_URI} !(.[^/]+|[^/]/+|^/admin.|^/$)$
RewriteRule . %{REQUEST_URI}/ [R,L]
The condition excludes the admin tree because without it I disabled the
“Clear Page Cache” function.
Bill