apache – Apply No Case Sensitive to web directories/folders

Question:

Some time ago I asked this question on the English site without getting an answer.

It happens that I am applying the RewriteRule [NC] rule to mysite.com so that, regardless of the writing, uppercase or lowercase, it accepts the request in the URI.

I have the following configuration in my .htaccess file

RewriteEngine On

RewriteCond %{SERVER_PORT} 80 [NC]

RewriteRule ^(.*)$ https://misitio.com/$1 [R,L,NC]

It works for the main domain, but not for folders or directories (eg mysite.com/contact )

According to the Apache documentation this instruction should work globally. This is what I have tried:

RewriteMap lowercase int:tolower RewriteCond %{HTTP_HOST} [A-Z] 

RewriteRule (.*) http://${lowercase:%{HTTP_HOST}}$1 [R,L,NC]

I saw the option to apply this rule to each directory independently, but pages are created dynamically and controlling them this way doesn't seem like the best option to me.

I also saw this answer on SOen , but I don't quite understand it to apply to my site. Any suggestion?

Thanks a lot.

Answer:

As discussed in askapache's Htaccess to Redirect Uppercase to Lowercase , there are at least three ways to force everything typed in the browser bar to be converted to lowercase.

Everything will depend on whether you have access to the Apache configuration file or not (generally in shared hosting they do not give you access to that file).

If you don't have access to the configuration file

Directly in the .htaccess file

In that case you can put these rules, keeping in mind that this should go at the top of your .htaccess file. It should at least override any other RewriteRule . This is because it uses a loop, until there are no more uppercase characters to convert, it will keep starting at the first rewrite rule of HASCAPS: TRUE . This is actually very fast and won't slow anything down.

I have this implemented on my site and it works without a problem. Although I haven't been able to get it to work on sub directories, it does work on the root of my site. My problem was that I created a program to save the number of visits to my site for each URL using the Google Analytics API and I couldn't do the filters correctly by URL when the user typed using any capital letters… currently everything that enters the site arrives in lowercase.

RewriteEngine On
RewriteBase /

# Si hay mayúsculas, establezca HASCAPS en verdadero y omita la siguiente regla
RewriteRule [A-Z] - [E=HASCAPS:TRUE,S=1]

# Omita esta sección completa si no hay letras en mayúsculas en la URL solicitada
RewriteRule ![A-Z] - [S=28]

# Reemplace la ocurrencia individual de CAP con cap, luego procese la siguiente Regla.
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

# Si hay letras en mayúsculas, reinicie en la primera RewriteRule en el archivo
RewriteRule [A-Z] - [N]

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

If you have access to the configuration file

You can do it in either of these two ways:

Using RewriteMap in httpd.conf file

This is technically a faster way to do it, but it has to be in the httpd.conf file, not .htaccess :

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

Using mod_speling

You can also enable the mod_speling apache module, as follows:

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

Other options

Since you say that in your context directories are created freely by users in some way, perhaps it would be advisable to control the code that creates those directories, causing the contents to be created in already fixed directories. As for URLs, you could also control that they are always created in lower case, for example, by PHP promotion always control that file names and extensions are converted to lower case before saving them on the server.

Scroll to Top