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

编程入门 行业动态 更新时间:2024-10-23 03:13:27
本文介绍了如何处理web.config重写规则中的特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

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

This URL www.example/bungalow/rent/state/texas/street/exloër/exloër with 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 www.example/bungalow/rent/state/texas/street/exloër/exloër遵循以下重写规则:

This URL www.example/bungalow/rent/state/texas/street/exloër/exloër with 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%abr With 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

根据评论示例:

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

Your url: www.example/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: www.urlencoder/.

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

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重写规则中的特殊字符?

本文发布于:2023-10-29 09:59:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539399.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重写   如何处理   特殊字符   规则   config

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!