Force or remove trailing slash?

I’m using a rewrite for friendly urls, if the page is accessed without a trailing slash it throws a 404 error.

Is it possible to either force the trailing slash so it is impossible to access it without a slash and get an error, or force remove the trailing slash and access url without it.

here is what i have so far

Options -Indexes -MultiViews +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_URI} ^
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)$ index.php?pages=$1&action=$2 [L]


Hi Uplift,

You might try this

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]

Thanks, it didnt seem to work


IMHO, the server should throw an error with a trailing / on a filename (you’re not using MultiViews) just the same way that the server should throw an error if you request a directory without the trailing / (servers have been written to compensate for this error, though).

Anything is possible and I’d say that it depends upon the server’s configuration. What you have seems …

Options -Indexes -MultiViews +FollowSymLinks  
RewriteEngine On

[COLOR="#808080"]RewriteCond %{REQUEST_URI} ^
# empty string? That seems useless to me[/COLOR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)$ index.php?pages=$1&action=$2 [L]
# That doesn't do what you're asking for ... and could cause problems with your EVERYTHING
# OR NOTHING metacharacter in both atoms. Why aren't you using + (one or more)?

Steve’s code could use a little help although, technically, it’s not wrong:

RewriteCond %{SCRIPT_FILENAME} !-d
# There is a subtle difference between {SCRIPT_FILENAME} and {REQUEST_FILENAME}
# with {REQUEST_FILENAME} being the preferred variable here
RewriteRule ^(.+)/$ [COLOR="#FF0000"]/[/COLOR]$1 [R=301,L]
# The leading / in the redirection tells Apache to look in the server's root directory
# before looking in your DocumentRoot - a very dangerous thing to do especially
# without validating the submitted {REQUEST_URI} string. However, for your
# solution, I'd merely omit the / and look for a way to eliminate hack attacks
# with the (.+) regex (like eliminating the dot character).

Saying that it doesn’t work doesn’t help. Please provide the test URI and what was returned from the server as well as the exact .htaccess you were using. We can’t help you troubleshoot without useful information.