Tarlan Mammadzada Tarlan Mammadzada - 1 month ago 18
Apache Configuration Question

.htaccess case-insensitive directories

I access to the

file.php
writing
mydomain.com/file
. I need to be able to access also writing
mydomain.com/FILE
.

In .htaccess I use the following rules to remove extension:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]

# redirect to .php-less link if requested directly
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.+)\.php $1 [R=301,L]


If this rules should be changed- it's up to you.

Answer

Define this RewriteMap in Apache or vhost config:

RewriteMap lc int:tolower

Then inside your .htaccess have an additional rule to lowercase all uppercase URIs:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301,NE]

# redirect to .php-less link if requested directly
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP [NC]
RewriteRule ^(.+)\.php$ /$1 [R=301,L,NC,NE]

# convert each REQUEST_URI to lowercase
RewriteRule ^(.*?[A-Z]+.*)$ /${lc:$1} [R=301,L,NE]

# internally add .php 
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Comments