Mobile app version of vmapp.org
Login or Join
Voss4911412

: How to redirect any URL to lowercase URL? I am using: <IfModule mod_rewrite.c> Options +FollowSymLinks RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com [NC]

@Voss4911412

Posted in: #301Redirect #Htaccess #ModRewrite #Url

I am using:

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule (.*) example.com/ [L,R=301]
</IfModule>


But I want to rewrite all uppercase characters to lowercase characters, e.g:


OLD: /myfolder-sample-URL.html
NEW: /myfolder-sample-url.html

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Voss4911412

2 Comments

Sorted by latest first Latest Oldest Best

 

@Nimeshi995

Add this to the top of your .htaccess


SOURCE: Htaccess to Redirect Uppercase to Lowercase

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(.*)$ a
RewriteRule ^([^B]*)B(.*)$ b
RewriteRule ^([^C]*)C(.*)$ c
RewriteRule ^([^D]*)D(.*)$ d
RewriteRule ^([^E]*)E(.*)$ e
RewriteRule ^([^F]*)F(.*)$ f
RewriteRule ^([^G]*)G(.*)$ g
RewriteRule ^([^H]*)H(.*)$ h
RewriteRule ^([^I]*)I(.*)$ i
RewriteRule ^([^J]*)J(.*)$ j
RewriteRule ^([^K]*)K(.*)$ k
RewriteRule ^([^L]*)L(.*)$ l
RewriteRule ^([^M]*)M(.*)$ m
RewriteRule ^([^N]*)N(.*)$ n
RewriteRule ^([^O]*)O(.*)$ o
RewriteRule ^([^P]*)P(.*)$ p
RewriteRule ^([^Q]*)Q(.*)$ q
RewriteRule ^([^R]*)R(.*)$ r
RewriteRule ^([^S]*)S(.*)$ s
RewriteRule ^([^T]*)T(.*)$ t
RewriteRule ^([^U]*)U(.*)$ u
RewriteRule ^([^V]*)V(.*)$ v
RewriteRule ^([^W]*)W(.*)$ w
RewriteRule ^([^X]*)X(.*)$ x
RewriteRule ^([^Y]*)Y(.*)$ y
RewriteRule ^([^Z]*)Z(.*)$ z

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

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



Alternatively your host may support mod_speling, in which if they do, you can use in the server config, virtual host, directory or within a .htaccess file, example below:

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

10% popularity Vote Up Vote Down


 

@Ann8826881

To implement such redirect using mod_rewrite and .htaccess you need to use RewriteMap directive which cannot be placed in .htaccess -- only in server config / VirtualHost context. If you have such access:

1. Place this line inside <VirtualHost> block for your site:

RewriteMap lc int:tolower


2. Place this in your .htaccess:

RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule . ${lc:%{REQUEST_URI}} [R=301,L]


This will redirect (301 Permanent Redirect) any URL that has at least 1 capital letter (Latin letters only) in path part of URL (query string is ignored) to the same but in lower case.

For example:
mydomain.com/myfolder-sample-URL.html => mydomain.com/myfolder-sample-url.html mydomain.com/myfolder-sample-url.HTML => mydomain.com/myfolder-sample-url.html

but will do nothing for these URLs:
mydomain.com/myfolder-sample-url.html?say=MEOW http://MYDOMAIN.com/myfolder-sample-url.html


If you want to restrict it to .html files only:

RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^.+.html$ ${lc:%{REQUEST_URI}} [NC,R=301,L]




Where to place: I would place it after domain name redirect rule:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule (.*) mydomain.com/ [L,R=301]

RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule . ${lc:%{REQUEST_URI}} [R=301,L]

# your other rules




I also recommend using rel="canonical" links:

<link rel="canonical" href="PROPER_URL_HERE" />


www.google.com/support/webmasters/bin/answer.py?answer=139394 http://googlewebmastercentral.blogspot.com/2009/02/specify-your-canonical.html




If you do not need redirect, but rather ability to serve the file regardless of the file name case (case insensitive file names), then you can use mod_spelling ( httpd.apache.org/docs/current/mod/mod_speling.html ) and CheckCaseOnly On directive.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme