# Adding the trailing slash to a page url

**URL:** <https://www.ruby-forum.com/t/adding-the-trailing-slash-to-a-page-url/128101>\
**Category:** Radiant CMS\
**Created:** [February 5, 2008, 1:52am UTC](https://www.ruby-forum.com/t/adding-the-trailing-slash-to-a-page-url/128101 "2008-02-05T01:52:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bburcham](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f9ae1b/32.png) [@bburcham](https://www.ruby-forum.com/u/bburcham)\
**Post date:** [February 5, 2008, 1:52am UTC](https://www.ruby-forum.com/t/adding-the-trailing-slash-to-a-page-url/128101/1 "2008-02-05T01:52:59Z")

</div>

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](http://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](http://example.com/dir/sub_page) instead of  
[example.com/dir/local\_page/sub\_page](http://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](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

---

<div class="post-metadata">

**Author:** ![bburcham](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f9ae1b/32.png) [@bburcham](https://www.ruby-forum.com/u/bburcham)\
**Post date:** [February 5, 2008, 5:07pm UTC](https://www.ruby-forum.com/t/adding-the-trailing-slash-to-a-page-url/128101/2 "2008-02-05T17:07:46Z")

</div>

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
