重写规则中的特殊字符

重写规则中的特殊字符

本文介绍了如何处理web.config重写规则中的特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

URL包含特殊字符时,我的重写规则出错:

I'm getting errors on my rewrite rules when the URL contains a special character:

此URL http://www.example.com/bungalow/rent/state/texas/street/exloër/exloër遵循以下重写规则:

This URL http://www.example.com/bungalow/rent/state/texas/street/exloër/exloërwith this rewrite rule:

    <rule name="rentals by proptype+state+city+street">
      <match url="^([a-zA-Z0-9-+]+)/rent/state/([a-zA-Z-+]+)/street/([a-zA-Zë-+]+)/([0-9a-zA-Zë-+']+)$" />
      <action type="Rewrite" url="search_new.aspx?proptype={R:1}&amp;state={R:2}&amp;city={R:3}&amp;street={R:4}" />
    </rule>

导致500错误

此URL http://www.example.com/bungalow/rent/state/texas/street/exloër/exloër遵循以下重写规则:

This URL http://www.example.com/bungalow/rent/state/texas/street/exloër/exloërwith this rewrite rule:

    <rule name="rentals by proptype+state+city+street">
      <match url="^([a-zA-Z0-9-+]+)/rent/state/([a-zA-Z-+]+)/street/([a-zA-Z-+]+)/([0-9a-zA-Z-+']+)$" />
      <action type="Rewrite" url="search_new.aspx?proptype={R:1}&amp;state={R:2}&amp;city={R:3}&amp;street={R:4}" />
    </rule>

导致404错误

如何处理重写规则中的特殊字符?

How can I handle special characters in the rewrite rule?

更新1

有问题的URL用ë字符显示,但是当我复制地址时,它被转义到此%c3%abr 使用此规则,我仍然会收到404错误:

The URL in question is displayed with a ë character, but when I copy the address, it's escaped to this %c3%abrWith this rule I still get a 404 error:

    <rule name="rentals by proptype+state+city+street">
      <match url="^([a-zA-Z0-9-+]+)/rent/state/([a-zA-Z-+]+)/street/([a-zA-Z%-+]+)/([0-9a-zA-Z%-+']+)$" />
      <action type="Rewrite" url="search_new.aspx?proptype={R:1}&amp;state={R:2}&amp;city={R:3}&amp;street={R:4}" />
    </rule>

所以我想真正的问题是,如何处理重写规则中的个字符?

So I guess the real question would be, how to handle % characters in the rewrite rule?

推荐答案

您最后一次尝试使用的正则表达式几乎正确,您只是犯了一个小错误(忘记在第三个代码块中添加0-9).正确的正则表达式为:

Your last attempt had almost correct regex, you just had one small mistake (forgot to add 0-9 to third block). Correct regexp is:

^([[a-zA-Z0-9-+] +)/租金/州/([a-zA-Z-Z-+] +)/街道/([a-zA-Z0-9%-+] +)/([[0-9a-zA-Z%-+'] +)$

但是在重写规则中,您需要使用变量 {UNENCODED_URL} .

But in rewrite rule you need to use variable {UNENCODED_URL}.

工作示例是:

<rule name="rentals by proptype+state+city+street" stopProcessing="true">
    <match url=".*" />
    <conditions>
        <add input="{UNENCODED_URL}" pattern="^/([a-zA-Z0-9-+]+)/rent/state/([a-zA-Z-+]+)/street/([a-zA-Z0-9%-+]+)/([0-9a-zA-Z%-+']+)$" />
    </conditions>
    <action type="Rewrite" url="search_new.aspx?proptype={C:1}&amp;state={C:2}&amp;city={C:3}&amp;street={C:4}" />
</rule>

UPD

根据评论示例:

您的网址: http://www.example.com/Bungalow/rent/state/north-dakota/stre ‌et/savanah/%27s-gr‌ac‌eland具有一些隐藏的特殊字符(即使这样也无法正确解析).您可以在此处检查其编码方式: https://www.urlencoder.org/.

Your url: http://www.example.com/bungalow/rent/state/north-dakota/stre‌​‌​et/savanah/%27s-gr‌​ac‌​eland has some hidden special characters (even SO can't parse it properly). You can check how it's encoded here: https://www.urlencoder.org/.

在此情况下,我使用以下命令更改了规则中的正则表达式:

Becased on that i changed the regexp in the rule with following:

<rule name="rentals by proptype+state+city+street" stopProcessing="true">
    <match url=".*" />
    <conditions>
        <add input="{UNENCODED_URL}" pattern="^/([a-zA-Z0-9\-+]+)/rent/state/([a-zA-Z\-+]+)/([a-zA-Z0-9%\-+]+)/([a-zA-Z0-9%\-+]+)/([0-9a-zA-Z%\-+']+)$" />
    </conditions>
    <action type="Rewrite" url="search_new.aspx?proptype={C:1}&amp;state={C:2}&amp;city={C:4}&amp;street={C:5}" />
</rule>

这篇关于如何处理web.config重写规则中的特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 07:16