Link to home
Start Free TrialLog in
Avatar of skij
skijFlag for Canada

asked on

.htaccess mod_rewrite: Case Insensitive Directory Names

Using mod_rewrite in .htaccess, how can I rewrite all directory names containing upper-case letters so that they only contain lower-case letters?  

For example:
/MyDir/xYz.html
should be rewritten as:
/mydir/xYz.html
Avatar of Zac Harris
Zac Harris
Flag of United States of America image

This will do it.

RewriteEngine On
RewriteMap  lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) ${lc:$1} [R=301,L]

Open in new window


Explanation:

1. The RewriteEngine is enabled, and a custom RewriteMap called ‘lc’ is defined using the internal ‘tolower’ map.

2. A RewriteCond rule is used so that only URLs containing upper-case characters are rewritten.

3. The actual RewriteRule is defined using the ‘lc’ RewriteMap that was just created to transform the captured URL to lower-case before 301-redirecting the user

Why the 301 redirect?

If you need to change the URL of a page as it is shown in search engine results, it's recommend that you use a server-side 301 redirect. This is the best way to ensure that users and search engines are directed to the correct page. The 301 status code means that a page has permanently moved to a new location.
Avatar of skij

ASKER

Zac, as specified in the question, this needs to be done using .htaccess.  

Because RewriteMap only works if used in the http.conf file, it is irrelevant to my needs.

I should also emphasize that only the directory name should be affected.  Even if I was able to use RewriteMap, the idea you provided would be applied to the page name as well as the directory name.
ASKER CERTIFIED SOLUTION
Avatar of Zac Harris
Zac Harris
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of skij

ASKER

I am going to accept this solution because it does provide a way to do it, even though it uses PHP.

I have asked a related question here because I do not want to use PHP for this:
https://www.experts-exchange.com/questions/28923401/mod-rewrite-Redirect-Uppercase-Directory-Names-Purely-with-htaccess.html
As I said, I could give you a way to do it purely in .htaccess but it adds additional processing to server. Using the method I provided cuts that down. I am more than happy to give you another way that will add additional processing time to the process.
Avatar of skij

ASKER

Yes, I am interested in a purely .htaccess solution, thanks.