Adding the trailing slash to a page url

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

For an Apache without Radiant you can use the DirectorySlash directive
of
mod_dir. With a Radiant/Rails app i don’t know how this directive will
behave but i guess that it will not work since incoming request are
passed
directly to Rails.

Other alternatives that spring to mind are:

  • use a custom extension that rewrites the URLs somehow, on page
    generation time or saving time
  • do the redirect in Javascript on the client side

/AITOR