ycx
ycx

Reputation: 31

Make Folders in Apache Case Insensitive using .htaccess

I need to make accessing directories on my server case insensitive.

How do I do that using htaccess?

Upvotes: 3

Views: 13515

Answers (4)

Reza Mamun
Reza Mamun

Reputation: 6189

Solution-1: To make case-insensitive directory and files names with respect to the requested URL we can add the following two lines to the app's .htaccess file:

<IfModule mod_speling.c>
    #Once enabled, mod_speling redirects misspelled requests to any nearest matching resources. Uses a bit of memory, but can be useful if you've been changing URIs or have lots of similarly named URIs:
    CheckSpelling On
    CheckCaseOnly on
</IfModule>

Solution-2: Again if we want just a few predefined directories to go to specific directories, then apply the below lines instead:

<IfModule mod_rewrite.c>
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^my-dir/old-dir-1/$ /my-dir/new-dir-1/ [R=301,L,NE]
    RewriteRule ^my-dir/old-dir-2/$ /my-dir/new-dir-2/ [R=301,L,NE]
    RewriteRule ^my-dir/old-dir-3/$ /my-dir/new-dir-3/ [R=301,L,NE]

    # For any case insensitive directories we can try adding NO-CASE (NC)
    RewriteRule ^My-Old-Dir/$ /my-new-dir/ [R=301,L,NC]
</IfModule>

Upvotes: 0

user221458
user221458

Reputation: 736

If you want requested URLs to be valid whether uppercase or lowercase letters are used, use mod_speling to make URLs case-insensitive. Write the following code in .htaccess file:

CheckSpelling On

Upvotes: 2

Tom
Tom

Reputation: 7091

This is what I used because my hosting is shared and does not include the mod_spelling module but does support .htaccess, but this only works for one folder:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^student-government/$ http://www.tombarrasso.com/Student-Government/ [R=302,NC,L]

The folder to redirect to can be any case, so you could use lower-case folders and redirect all variations of spelling there.

I suppose it could be adapted with a little bit of REGEX to work for all folders rather than just one. This worked for me on Apache 2.2.14 (Unix).

Upvotes: 1

frankjacobi
frankjacobi

Reputation: 31

You have to install and enable the mod_speling module in apache and set the CheckCaseOnly Directive to On in your .htaccess

CheckCaseOnly On

Upvotes: 3

Related Questions