如何处理 web.config 重写规则中的特殊字符?
How to handle special characters in web.config rewrite rule?
当 URL 包含特殊字符时,我的重写规则出现错误:
这个URLhttp://www.example.com/bungalow/rent/state/texas/street/exloër/exloër
使用此重写规则:
<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}&state={R:2}&city={R:3}&street={R:4}" />
</rule>
导致 500 错误
这个URLhttp://www.example.com/bungalow/rent/state/texas/street/exloër/exloër
使用此重写规则:
<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}&state={R:2}&city={R:3}&street={R:4}" />
</rule>
导致 404 错误
如何处理重写规则中的特殊字符?
更新 1
有问题的 URL 显示为 ë
字符,但是当我复制地址时,它被转义为 %c3%abr
使用此规则我仍然会收到 404 错误:
<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}&state={R:2}&city={R:3}&street={R:4}" />
</rule>
所以我想真正的问题是,如何处理重写规则中的 %
个字符?
您上次尝试的正则表达式几乎是正确的,您只是犯了一个小错误(忘记在第三个块中添加 0-9)。正确的正则表达式是:
^([a-zA-Z0-9-+]+)/rent/state/([a-zA-Z-+]+)/street/([a-zA-Z0-9%-+]+)/([0-9a-zA-Z%-+']+)$
但是在重写规则中你需要使用变量{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}&state={C:2}&city={C:3}&street={C:4}" />
</rule>
UPD
根据评论中的示例:
你的url:http://www.example.com/bungalow/rent/state/north-dakota/street/savanah/%27s-graceland has some hidden special characters (even SO can't parse it properly). You can check how it's encoded here: https://www.urlencoder.org/。
因为我更改了规则中的正则表达式,如下所示:
<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}&state={C:2}&city={C:4}&street={C:5}" />
</rule>
当 URL 包含特殊字符时,我的重写规则出现错误:
这个URLhttp://www.example.com/bungalow/rent/state/texas/street/exloër/exloër 使用此重写规则:
<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}&state={R:2}&city={R:3}&street={R:4}" />
</rule>
导致 500 错误
这个URLhttp://www.example.com/bungalow/rent/state/texas/street/exloër/exloër 使用此重写规则:
<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}&state={R:2}&city={R:3}&street={R:4}" />
</rule>
导致 404 错误
如何处理重写规则中的特殊字符?
更新 1
有问题的 URL 显示为 ë
字符,但是当我复制地址时,它被转义为 %c3%abr
使用此规则我仍然会收到 404 错误:
<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}&state={R:2}&city={R:3}&street={R:4}" />
</rule>
所以我想真正的问题是,如何处理重写规则中的 %
个字符?
您上次尝试的正则表达式几乎是正确的,您只是犯了一个小错误(忘记在第三个块中添加 0-9)。正确的正则表达式是:
^([a-zA-Z0-9-+]+)/rent/state/([a-zA-Z-+]+)/street/([a-zA-Z0-9%-+]+)/([0-9a-zA-Z%-+']+)$
但是在重写规则中你需要使用变量{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}&state={C:2}&city={C:3}&street={C:4}" />
</rule>
UPD
根据评论中的示例:
你的url:http://www.example.com/bungalow/rent/state/north-dakota/street/savanah/%27s-graceland has some hidden special characters (even SO can't parse it properly). You can check how it's encoded here: https://www.urlencoder.org/。
因为我更改了规则中的正则表达式,如下所示:
<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}&state={C:2}&city={C:4}&street={C:5}" />
</rule>