1

I've spent a bit of time attempting different methods for adding trailing slashes to my URLs, such as this one:

Add Trailing Slash to URLs

On my production site, this is working fine with this rule:

RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*[^/])$ /$1$2/ [R=301,L]

But it's not working on my local development setup. I run the local sites at localhost:9090, so when I go to an URL such as localhost:9090/users/new/ I get a 500 Internal Server Error.

I've tried a similar rule using %{HTTP_POST} and this doesn't work either. I feel like I'm missing something and searching the web keeps pointing me to rules I've already tried.

What is the best way to have a rule that works for both development and production?

NOTE

After some great advice from @anubhava, I reorganized my rules and found some better rules for existing needs. I ended up going with a new PHP extension rule and his recommendation and it's working now on both localhost and production.

Community
  • 1
  • 1
nicorellius
  • 3,715
  • 4
  • 48
  • 79

1 Answers1

1

There is no $2 in your regex.

Can you try this code:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?[^/])$ /$1/ [R=302,L]

# hiding .php
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{DOCUMENT_ROOT}/$1.php -f 
RewriteRule ^(.+?)/?$ /$1.php [L]

Try it out in a new browser to avoid 301 caching issues.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • I've tried `/$1/` and `/$1$2/` in an attempt to cover multiple levels of directories. Both don't work on new browser. Should `/$1/` cover URLs like `example.com/test/dir/path/`? – nicorellius Nov 01 '13 at 20:33
  • Run this in Firebug and see what you get in NEt tab – anubhava Nov 01 '13 at 20:34
  • Run in Chrome, under Firebug > Network: GET method, 500 internal server error, type: text/html – nicorellius Nov 01 '13 at 20:35
  • Yes, I've tried my original with only `/$1/` and your exact rule. Both are giving 500 on new browsers with no cached 301s. In reviewing logs, there might be something valuable in there... Looking now. – nicorellius Nov 01 '13 at 20:40
  • Do you have more rules in .htaccess? OR any other .htaccess? – anubhava Nov 01 '13 at 20:43
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40401/discussion-between-nicorellius-and-anubhava) – nicorellius Nov 01 '13 at 20:44