Apache rewrite all URLs to lowercase if contains at least one uppercase

11,793

There are multiple ways you can do this:

In httpd.conf (not in a .htaccess file):

RewriteEngine on
RewriteBase /
RewriteMap lowercase int:tolower
RewriteCond $1 [A-Z]
RewriteRule ^/?(.*)$ /${lowercase:$1} [R=301,L]

Or in .htaccess (YMMV with performance) - note that the S=28 means to skip the following 28 rules (as per the Apache documentation):

RewriteEngine On
RewriteBase /

# If there are caps, set HASCAPS to true and skip next rule
RewriteRule [A-Z] - [E=HASCAPS:TRUE,S=1]

# Skip this entire section if no uppercase letters in requested URL
RewriteRule ![A-Z] - [S=28]

# Replace single occurance of CAP with cap, then process next Rule.
RewriteRule ^([^A]*)A(.*)$ $1a$2
RewriteRule ^([^B]*)B(.*)$ $1b$2
RewriteRule ^([^C]*)C(.*)$ $1c$2
RewriteRule ^([^D]*)D(.*)$ $1d$2
RewriteRule ^([^E]*)E(.*)$ $1e$2
RewriteRule ^([^F]*)F(.*)$ $1f$2
RewriteRule ^([^G]*)G(.*)$ $1g$2
RewriteRule ^([^H]*)H(.*)$ $1h$2
RewriteRule ^([^I]*)I(.*)$ $1i$2
RewriteRule ^([^J]*)J(.*)$ $1j$2
RewriteRule ^([^K]*)K(.*)$ $1k$2
RewriteRule ^([^L]*)L(.*)$ $1l$2
RewriteRule ^([^M]*)M(.*)$ $1m$2
RewriteRule ^([^N]*)N(.*)$ $1n$2
RewriteRule ^([^O]*)O(.*)$ $1o$2
RewriteRule ^([^P]*)P(.*)$ $1p$2
RewriteRule ^([^Q]*)Q(.*)$ $1q$2
RewriteRule ^([^R]*)R(.*)$ $1r$2
RewriteRule ^([^S]*)S(.*)$ $1s$2
RewriteRule ^([^T]*)T(.*)$ $1t$2
RewriteRule ^([^U]*)U(.*)$ $1u$2
RewriteRule ^([^V]*)V(.*)$ $1v$2
RewriteRule ^([^W]*)W(.*)$ $1w$2
RewriteRule ^([^X]*)X(.*)$ $1x$2
RewriteRule ^([^Y]*)Y(.*)$ $1y$2
RewriteRule ^([^Z]*)Z(.*)$ $1z$2

# If there are any uppercase letters, restart at very first RewriteRule in file.
RewriteRule [A-Z] - [N]

RewriteCond %{ENV:HASCAPS} TRUE
RewriteRule ^/?(.*) /$1 [R=301,L]

Or using mod_spelling (your configuration suggests you're using Windows so YMMV here also):

<IfModule mod_speling.c>
  CheckCaseOnly On
  CheckSpelling On
</IfModule>

Source: http://www.askapache.com/htaccess/rewrite-uppercase-lowercase.html

Share:
11,793

Related videos on Youtube

BigB8767
Author by

BigB8767

Updated on September 18, 2022

Comments

  • BigB8767
    BigB8767 over 1 year

    I have been trying to get Apache to rewrite all URL's that contain an uppercase character anywhere in it to be lowercase. All the solutions I have found do not work in my case for some reason. I am hoping someone here can help me track down why.

    What I want is that if someone clicks a URL on our page that is like so http://example.com/products/productID/1234 they get sent to http://example.com/products/productid/1234 instead. Notice the ID is lowercase. It will not always be just ID either I want any upper case character converted to lowercase.

    Basically any URL I visit does not get changed at all which leads me to believe that it is not even attempting to rewrite them. I have verified the rewrite_module is un-commented as well.

    I have tried the answers here: Convert and redirect URL in uppercase to lowercase using .htaccess

    Excerpt of original httpd.conf

    <VirtualHost *:80>
      ServerName dev.xxxxxxx.com
      DirectoryIndex default.cfm index.cfm index.htm index.html
      DocumentRoot "Z:/XXXXX"
      ServerAdmin [email protected]
    
      Redirect 301 /xxxxxxxxx/photoFiles https://www.xxxxxxxxx.com/xxxxxxxx/photoFiles
      ProxyPreserveHost On
      # ProxyPassMatch .*\.(jpg|png|gif|css|js|ico|htm|txt|csv|html|pdf|doc|docx|xls|xlsx)$ !
      # ProxyPass / balancer://nodes/ stickysession=JSESSIONID|jsessionid scolonpathdelim=on
      ProxyPassMatch ^/(.+\.cf[cm])(/.*)?$ ajp://dev.xxxxxxxx.com:7009/$1$2
      # ErrorDocument 404 /errors/404.cfm
    </VirtualHost>
    

    Excert of updated (non-working) httpd.conf

    <VirtualHost *:80>
      ServerName dev.xxxxxxx.com
      DirectoryIndex default.cfm index.cfm index.htm index.html
      DocumentRoot "Z:/XXXXX"
      ServerAdmin [email protected]
    
      RewriteEngine On
      RewriteMap  lc int:tolower
      RewriteCond %{REQUEST_URI} [A-Z]
      RewriteRule (.*) ${lc:$1} [R=301,L]
    
      Redirect 301 /xxxxxxxxx/photoFiles https://www.xxxxxxxxx.com/xxxxxxxx/photoFiles
      ProxyPreserveHost On
      # ProxyPassMatch .*\.(jpg|png|gif|css|js|ico|htm|txt|csv|html|pdf|doc|docx|xls|xlsx)$ !
      # ProxyPass / balancer://nodes/ stickysession=JSESSIONID|jsessionid scolonpathdelim=on
      ProxyPassMatch ^/(.+\.cf[cm])(/.*)?$ ajp://dev.xxxxxxxx.com:7009/$1$2
      # ErrorDocument 404 /errors/404.cfm
    </VirtualHost>
    
  • BigB8767
    BigB8767 almost 8 years
    Unfortunatley it is still not working. I believe it has something to do with us using ProxyPass. We use Apache in front of TomCat which is why we are using ProxyPass,
  • DevLime
    DevLime almost 6 years
    Apache noob here. I'm seeing this answer a lot (clearly the right one) but this doesn not work for me. Does the rewrite rule need to be placed in a particualr place in the httpd.conf file or wrapped in a particular tag? Using the .htaccess answer just stops the page loading, sitting and spinning.