mod-rewrite:替换 url 中的一些字符

Yur*_*sov 3 mod-rewrite apache-2.2

是否可以在 RewriteRule 中将 URL 的一些正斜杠 (/) 替换为点 (.)?它不必专门用 RewriteRule 来完成,但绝对不是用脚本来完成的。

示例 1:

输入:/document/my/document.html
输出:/document-my.document.html

示例 2:

输入:/document/depth/of/path/can/vary.html
输出:/document-depth.of.path.can.vary.html

Jon*_*rke 5

我认为您可以使用迭代方法来做到这一点。“替换次数可变”意味着您必须多次使用相同的规则,每个“/”替换一次。

尝试这个:

RewriteRule ^/([^/]+)/(.*)$ /$1.$2 [N]
Run Code Online (Sandbox Code Playgroud)

一些细节:

  • 模式匹配/+ 任何东西 + /+ 任何东西
  • 您需要/明确地匹配第一个,因为它始终存在并且不能被替换为.
  • [N]标志的意思是:(Re-run the rewriting process (starting again with the first rewriting rule). This time, the URL to match is no longer the original URL, but rather the URL returned by the last rewriting rule.来自Apache mod_rewrite 文档