我想知道是否有办法在重写规则(iis和apache)中处理包含斜杠(%2F)作为其一部分的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>
或在apache中:
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
提前谢谢
答案 0 :(得分:1)
我发现你的情况很奇怪,因为在Apache的 mod_rewrite 模块中(我不确定IIS),它声明RewriteRule 模式与之匹配,我引用,“(% - 已解码)URL路径(或文件路径,取决于上下文)请求”。
我在测试后遇到的问题是编码的斜杠是不解码或服务器没有解释(我也得到404错误)。
但是,当内容用作查询字符串参数(例如http://www.example.com/?path=word1%2Fword2
)时,我意识到一个主要是url编码URL部分,这是合乎逻辑的,因为你不想要服务器将编码的斜杠解释为URL路径的一部分。请注意,这一观察结果与上述陈述相矛盾,因此请加以研究。
但我可以确认的是,重写www.domain.com/project/word1/word2
没问题。因此,我建议您不要对将在路径部分中使用的URL进行编码,但可能会将允许的字符列入白名单,以避免使用?
等特殊字符。
<强> htaccess的:强>
RewriteRule ^(.+) index.php?word=$1 [L]
<强>的index.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)
?>