Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

mod_rewrite syntax?!?

config for rewriting url's without trailing slash and all lower-case



7:48 pm on Apr 5, 2005 (gmt 0)

Inactive Member
Account Expired


This may be a simple question, but I'm having trouble understanding the syntax for mod_rewrite. I'm running a site that has tons of aliases and redirects, but people always type them in wrong. They either use the wrong case, or they add a trailing slash to a redirect that points to a file. I want to add a rewrite rule that will remove all trailing slashes, and convert the url case to lowercase no matter what a user enters into the URL field. Can someone PLEASE help me?
8:20 pm on Apr 5, 2005 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
votes: 0


Welcome to WebmasterWorld!

See the description of RewriteMap in mod_rewrite [httpd.apache.org]. It can call the system "tolower" function to force lowercase. RewriteMap is available in server context, and is not available in per directory (.htaccess) context. Then there is mod_speling [httpd.apache.org] which can correct a few errors in the URL, but at the cost of much slower server performance.

An alternative is to create a custom error document using php or similar, and use it to figure out what URL most closely matches the one that was typed-in. You can use mod_rewrite to check for "file exists" and steer all non-existent requests to this script. If it can fix the URL, fine, then return a 301 redirect to the corrected URL. If not, return a 404-Not Found. Doing this correctly will keep you out of trouble with the search engines.

As far as removing a trailing slash, that's a simple rewrite, and I recommend the documentation cited in our forum charter [webmasterworld.com] to you.

Other than that, take a hard look at your site navigation and try to make it more user-friendly, so that you get fewer type-ins, and pages are easier to find if you issue a 404 and then meta-refresh them to your home page to start over.