1

我想知道是否有一种方法可以处理包含斜杠( %2F )的重写规则( iis 和 apache )url查询字符串作为它的一部分。

举个例子:

www.domain.com/project/word1

被重写为

www.domain.com/project/index.php?word=word1

通过此规则(在 iis 中):

<rule name="Friendly">
    <match url="^(.+)$" ignoreCase="true" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="index.php?word={R:1}" appendQueryString="false" />
</rule>

或在阿帕奇:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.+)$ index.php?word=$1

这工作正常。

但是也有这样的情况:

www.domain.com/project/word1%2Fword2

应该重定向到

www.domain.com/project/index.php?word=word1/word2

但显然由于斜杠( %2F ),我得到了一个错误 404。有没有办法解决这个问题?即使这意味着我必须切断 /word2 部分并将 www.domain.com/project/word1%2Fword2 重定向到 www.domain.com/project/index.php?word=word1

先感谢您

4

1 回答 1

1

我发现您的情况很特殊,因为在 Apache 的mod_rewrite模块(我不确定 IIS)中,它声明RewriteRule 模式与我引用的“(%-decoded)URL路径(或文件路径,取决于请求的上下文)”

我在测试后遇到的是编码的斜杠没有被服务器解码或解释(我也得到一个 404 错误)。

但是,当内容将用作查询字符串参数(例如http://www.example.com/?path=word1%2Fword2)时,我意识到一个主要是 url 编码 URL 部分,这是合乎逻辑的,因为您不希望服务器将编码的斜杠解释为 URL 路径的一部分。请注意,此观察结果与上述陈述相矛盾,因此请持保留态度。

解决方案

然而,我可以确认的是重写没有问题www.domain.com/project/word1/word2。因此,我建议您不要对将在路径部分使用的 URL 的 pat 进行编码,而是可能将允许的字符列入白名单,以便避免使用特殊字符,例如?.


试验结果

.ht 访问:

RewriteRule ^(.+) index.php?word=$1 [L]

索引.php:

<pre>
    <?php var_dump( $_GET, true ); ?>
</pre>

网址:http://test/word1%2Fword2

产量 404。

网址:http://test/word1/word2

产量:

<?php
array (size=1)
  'word' => string 'word1/word2' (length=11)
?>
于 2013-02-26T09:01:57.013 回答