views:

23

answers:

2

Could somebody assist me in formulating the following .htaccess rule:

if an incoming request contains a top-level directory path like

 www.example.com/dirname

automatically convert this to

 www.example.com/dirname/

but only for the top level, so requests to

 www.example.com/dirname/subdirname

remain untouched.

Do I need mod_rewrite for this, or is there a simpler way?

+1  A: 

You could use the Alias directive if the amount of directives is small enough. Otherwise you would have to go with mod_rewrite and some simple rules.

RewriteRule ^([^/]+)$ /$1/ [L]
aefxx
Cheers, that did the trick!
Pekka
Consider accepting my answer? :D
aefxx
Huh? Sorry, I thought I had. Maybe I clicked twice.
Pekka
Glad I could help. :D
aefxx
Won't this add a `/` after a GET variable? I think it would make `example.com/dirname?stuff=junk` into `example.com/dirname?stuff=junk/` which may throw off the actual query. Or am I wrong?
Anthony
A: 

To rewrite a URL, you need to use mod_rewrite. In this case, I would go with something that looks for anything after the initial / that isn't ? . # / and add a / after that. Maybe some other SO's will point out other tokens that I am leaving out.

 RewriteRule ^([^/./?//#]+)?$ $1/ [L]
Anthony