【问题标题】:How to replace escaped slashes via mod_rewrite?如何通过 mod_rewrite 替换转义的斜杠?
【发布时间】:2019-06-05 07:01:00
【问题描述】:

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

之类的网址

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

我已经尝试了以下方法:

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

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

谁能给我一个提示如何解决这个问题。

【问题讨论】:

    标签: apache mod-rewrite httpd.conf


    【解决方案1】:
    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]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-19
      • 2012-05-31
      • 1970-01-01
      • 2014-09-09
      • 1970-01-01
      • 2023-03-16
      • 2011-09-06
      • 2021-11-26
      相关资源
      最近更新 更多