使用 .htaccess 使 Apache 中的文件夹不区分大小写

发布于 2024-08-17 10:51:18 字数 59 浏览 3 评论 0原文

我需要使服务器上的访问目录不区分大小写。

我该如何使用 htaccess 来做到这一点?

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

How do I do that using htaccess?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忆伤 2024-08-24 10:51:18

您必须在 apache 中安装并启用 mod_speling 模块,并在 .htaccess 中将 CheckCaseOnly 指令设置为 On

CheckCaseOnly On

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

CheckCaseOnly On
深海夜未眠 2024-08-24 10:51:18

如果您希望请求的 URL 无论使用大写还是小写字母都有效,请使用 mod_speling 使 URL 不区分大小写。在.htaccess文件中写入以下代码:

CheckSpelling 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
忆梦 2024-08-24 10:51:18

这是我使用的,因为我的主机是共享的,不包含 mod_spelling 模块,但支持 .htaccess,但这仅适用于一个文件夹:

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

要重定向到的文件夹可以是任何情况,因此您可以使用小写文件夹和在那里重定向所有拼写变体。

我想它可以通过一点正则表达式进行调整,以适用于所有文件夹,而不仅仅是一个文件夹。这对我在 Apache 2.2.14 (Unix) 上有用。

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

墟烟 2024-08-24 10:51:18

解决方案-1:要使请求的 URL 的目录和文件名不区分大小写,我们可以将以下两行添加到应用程序的 .htaccess 文件中:

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

解决方案-2:
同样,如果我们只想将一些预定义的目录转到特定目录,则应用以下行:

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文