How to Force Lowercase URLs in Apache Using .htaccess Redirect Rules


10 views

When working with Apache web servers, inconsistent URL casing can create duplicate content issues and broken links. While some systems treat /A/B and /a/b as identical, others (particularly Windows servers) may see them as different resources. Let's implement a robust solution using mod_rewrite.

This rule checks for uppercase letters in the URL path and redirects to lowercase:

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

The solution uses three key Apache modules:

  • mod_rewrite: For URL rewriting capabilities
  • mod_map: For the lowercase conversion function
  • mod_alias: For the redirect functionality

For more control, you can modify the rule to exclude certain paths or file extensions:

RewriteEngine On
RewriteMap lowercase int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteCond %{REQUEST_URI} !^/exclude [NC]
RewriteCond %{REQUEST_URI} !\.(jpg|png|gif)$ [NC]
RewriteRule ^(.*)$ ${lowercase:$1} [R=301,L]

After implementing the rules:

  1. Clear your browser cache
  2. Test with various URL cases (/TEST/Page, /Test/Page)
  3. Verify the 301 redirect occurs
  4. Check server logs for rewrite processing

The rewrite map approach is generally efficient, but for high-traffic sites:

  • Place the rule early in your .htaccess file
  • Consider adding caching headers
  • Monitor server load after implementation

When dealing with web applications, URL case sensitivity can create duplicate content issues and broken links. Apache servers treat /A/B and /a/b as different resources by default, which isn't ideal for most applications.

Here's a complete solution that redirects uppercase URLs to their lowercase equivalents while preserving query strings:

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

This configuration:

  • Activates Apache's rewrite engine
  • Creates a lowercase conversion map
  • Checks for uppercase characters in the URI
  • Performs a 301 redirect to the lowercase version

For more complex scenarios including query strings:

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

The QSA flag ensures any query parameters (?param=value) are preserved during the redirect.

When implementing this solution:

  • Use 301 (permanent) redirects for SEO benefits
  • Place these rules near the top of your .htaccess file
  • Test thoroughly in development before production deployment
  • Consider combining with other URL normalization rules

If the redirect isn't working:

  1. Verify mod_rewrite is enabled in Apache
  2. Check for syntax errors in .htaccess
  3. Ensure the rules are in the correct order
  4. Test with different browser sessions (clear cache)
# Debugging tip: Add this to check rewrite processing
RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 3