Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top