apache 带有 mod_rewrite 的不区分大小写的 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1998156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:33:58  来源:igfitidea点击:

Case Insensitive URLs with mod_rewrite

apacheurlmod-rewrite

提问by Paul Tarjan

I'd like for any url that doesn't hit an existing file, to do a lookup on the other possible cases and see if those files exist, and if so, 302 to them.

我想要任何未命中现有文件的 url,对其他可能的情况进行查找并查看这些文件是否存在,如果存在,则向它们发送 302。

If that's not possible, then I'm ok with these compromises:

如果这是不可能的,那么我可以接受这些妥协:

  • Only check the lowercase version
  • Only check the first path portion
  • 只检查小写版本
  • 只检查第一个路径部分

For example http://example.com/CoOl/PaTH/CaMELcaSEshould redirect to http://example.com/cool/path/camelCase(assuming the latter exists).

例如http://example.com/CoOl/PaTH/CaMELcaSE应该重定向到http://example.com/cool/path/camelCase(假设后者存在)。

but of course a full solution is much more useful to me and others

但当然,完整的解决方案对我和其他人更有用

回答by fuxia

CheckSpelling on

Matches files and directories. See the documentationfor details.

匹配文件和目录。有关详细信息,请参阅文档

回答by Maelstrom

I don't have Apache handy to test, but some combination of these rules should do what you want:

我没有方便测试的 Apache,但是这些规则的某种组合应该可以满足您的需求:

RewriteEngine on
RewriteMap lower int:tolower
RewriteCond ${lower:%{REQUEST_URI}} -U
RewriteRule [A-Z] ${lower:%{REQUEST_URI}} [R=302,L]
  • A lowercase map to convert /SoMeThinG to /something
  • A condition to see if the lowercase of the REQUEST_URI exists (-U is internal apache query)
  • The rule to actually do the rewrite
  • 将 /SomeThinG 转换为 /something 的小写映射
  • 查看REQUEST_URI小写是否存在的条件(-U是apache内部查询)
  • 实际进行重写的规则

I don't know if the RewriteMap can be applied in a condition, or if it only applies to a rule. These are based on experts exchange accepted answerand a small orange forum discussion.

我不知道 RewriteMap 是否可以应用于条件,或者是否仅适用于规则。这些是基于专家交流接受的答案小橙子论坛的讨论

Your "ideal" solution is probably not possible unless you can enumerate every valid page on your site. If you only have a few valid pages, a combination of RewriteMapand a text map will do exactly what you need. If there are hundreds / thousands of pages you may need to write a script and use the prgdirective.

除非您可以枚举站点上的每个有效页面,否则您的“理想”解决方案可能是不可能的。如果您只有几个有效页面,则RewriteMap和文本映射的组合将完全满足您的需求。如果有成百上千的页面,您可能需要编写脚本并使用该prg指令。

If you can't identify every valid page, you would need to try every variant in case. Consider your URL as a binary string, with 0 for lowercase letter and 1 for uppercase. Just from your simple example you'd have to test 2^17 variations, 128k pages.

如果您无法识别每个有效页面,则需要尝试每个变体以防万一。将您的 URL 视为二进制字符串,0 代表小写字母,1 代表大写字母。仅从您的简单示例来看,您必须测试 2^17 个变体,128k 页。

回答by Emil Vikstr?m

Look up the Apache module mod_negotiation. It does exactly what you want: http://httpd.apache.org/docs/2.0/mod/mod_negotiation.html#multiviews

查找 Apache 模块 mod_negotiation。它完全符合您的要求:http: //httpd.apache.org/docs/2.0/mod/mod_negotiation.html#multiviews

You can also pipe all requests to a single PHP file and let the PHP file do the checking for you.

您还可以将所有请求通过管道传输到单个 PHP 文件,并让 PHP 文件为您进行检查。