如何通过mod_rewrite替换转义斜杠



我正在将一个带有奇怪 php 脚本的旧 Web 服务器迁移到具有标准目录浏览的 apache httpd 服务器。旧脚本需要像 http://myserver/index.php?fm_dir=dir1%2Fsubdir1%2Fsubdir2 这样的网址现在应该用类似 http://myserver/dir1/subdir1/subdir2 的 url 替换

为了便于迁移,我尝试通过 mod 重写重写 url。

我已经尝试了以下方法:

RewriteCond %{QUERY_STRING} ^fm_dir=(.*)$
RewriteRule index.php$ /%1? [R]

但是转义的斜杠仍然被转义,我得到和 404 (http://myserver/dir1%2Fsubdir1%2Fsubdir2)。

有人可以给我一个提示如何解决这个问题。

RewriteCond %{QUERY_STRING} ^fm_dir=(.*)$
RewriteRule index.php$ /%1? [NE,N]
# as long as there are two or more slashes, 
# replace only one and keep looping internally
RewriteRule ^(.*)%2F(.*)%2F(.*) $1/$2%2F$3 [NE,N]
# when there is only one left, 
# replace that last one and send redirect to the client
RewriteRule ^(.*)%2F(.*) $1/$2 [L,R=302]

最新更新