Adding vs. Removing Slashes

Some search engines remove the trailing slash in listings from urls that look like directories (Yahoo does this).
Browsers can also alter the appearance of the URL (IE, Chrome and FF hides the slash after the root domain name.)

Sample htaccess file

# A security feature that must be enabled for the rewrite rules to work. This is
# probably defined on your web host, but it does not hurt to include it again.
Options +FollowSymLinks

# Turn-on the rewrite engine.
RewriteEngine On

# Set the base for the path.
#RewriteBase /

# Enforce trailing slash on requests that are not for files.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R]

# If a request does not match a real file or directory, rout it to `/index.php`.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

More Samples

# If the request IS NOT for a file:
RewriteCond %{REQUEST_FILENAME} !-f
If the request IS NOT for the specific file `file_name_to_exclude.php`:
RewriteCond %{REQUEST_URI} !file_name_to_exclude.php
# If the request IS for `.html` send it to `.php`:
RewriteRule ^(.*).html /$1.php [R=301,L]


R=301 can make many browsers cache the .htaccess file indefinitely: It somehow becomes irreversible if you can’t clear the browser-cache on all machines that opened it. When testing, better go with simple [L,R] or [L,R=302].


Apache doesn’t actually support line-end comments. It will “work” sometimes but could potentially break horribly, so best avoided:

RewriteRule ^(.*).html /$1.php [L,R] # <- for test, for prod use [L,R=301]