开发者

Make Folders in Apache Case Insensitive using .htaccess

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

How do I do that usi开发者_如何学运维ng htaccess?


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

CheckCaseOnly On


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


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


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜