3

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

How do I do that using htaccess?

4 Answers 4

3

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

CheckCaseOnly On
4
  • ironic. agent0074, is mod_spelling not enabled by default? I have a shared hosting account, so I don't have access to changing Apache modules.
    – ycx
    Jan 7, 2010 at 16:31
  • @ycx: Looks like it is disabled by default. httpd.apache.org/docs/2.2/mod/mod_speling.html
    – gameover
    Jan 7, 2010 at 16:34
  • 2
    It's really called "mod_speling" httpd.apache.org/docs/2.2/mod/mod_speling.html If it's enabled by default depends on your shared hosting provider, but don't think so, because it slows the server down a bit. Jan 7, 2010 at 16:37
  • I can't enable mod_speling. Is there not a Regex logic I can use?
    – ycx
    Jan 7, 2010 at 17:45
2

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
1

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).

0

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.