By: Steven Seiller
Page 2 of 2
Previous
Solving The Trailing Slash Problem
To make the redirection universal we will add another rewrite rule which precedes the solution to add the trailing slash when it is missing. The following script adds the trailing slash:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(/)$
RewriteRule (.*)$ /sample/$1/ [R,L]
The first two lines of our script sets conditions for the request. Particularly, we are checking to ensure that the requested file name is neither an existing file name nor an existing directory. The third condition ensures that the request does not already have a trailing slash. Should the request meet all three of these conditions, our rewrite rule takes the request and rewrites it within our subdirectory with the trailing slash. Now that this precedes our solution rewrite rule, all requests are properly rewritten whether they contain a trailing slash or not.
The attached .htaccess file contains the entire script. Place this file within the /sample directory to act upon your specific scenario. For applicable requests, this process will create an additional request for each rewrite, but will serve your redirection needs. If you placed this in the root directory, it will unnecessarily process requests which do not need processing and delay the server response to the visitor.
Conclusion
An infinite redirection loop will be your likely result when creating rewrite rules for URLs without a trailing slash. This tutorial showed you how to remedy this problem by creating a preceding rewrite condition which added the trailing slash to the requested URL.

Downloads are disabled during your trial period.
Keywords
Apache, mod_rewrite, htaccess, grep, generalized regular expression, RewriteRule, Redirect, RewriteCond


