Intelligencia URL 重写问题

发布于 2024-10-08 21:10:20 字数 342 浏览 3 评论 0原文

我在 web.config 中创建了 2 个 url 重写规则,如下所示:

rewrite url="~/Products/(.+).aspx" to="~/Products.aspx?Cat=$1"

rewrite url=" ~/Products/(.+)/(.+).aspx" to="~/Products.aspx?Cat=$1&SubCat=$2"

如果我​​输入 Products/xyz.aspx 它工作正常,但如果我尝试实现第二条规则(如 Products/xyx/abc.aspx)的 url 将 xyz 和 abc 传递给 Cat 而不是 SubCat。有什么想法可以让它同时处理这两者吗?

I have created 2 url rewrite rules in my web.config that look like the following:

rewrite url="~/Products/(.+).aspx" to="~/Products.aspx?Cat=$1"

rewrite url="~/Products/(.+)/(.+).aspx" to="~/Products.aspx?Cat=$1&SubCat=$2"

If i type in Products/xyz.aspx it works perfectly but if i try a url that implements the second rule like Products/xyx/abc.aspx it passes both xyz and abc to the Cat and not the SubCat. Any ideas how i can get it to handle both?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北方的韩爷 2024-10-15 21:10:20

我建议删除“.”上的所有匹配项。符号,因为它会让你的比赛更难预测。相反,使用涵盖您的产品类别和子类别的任何字符模式。

rewrite url="~/Products/([a-zA-Z0-9]+).aspx" to="~/Products.aspx?Cat=$1"

rewrite url="~/Products/[a-zA-Z0-9]+)/[a-zA-Z0-9]+).aspx" to="~/Products.aspx?Cat=$1&SubCat=$2"

I suggest removing all the matching on the "." symbol as it will make your matches a bit harder to predict. Instead use whatever character patterns cover your product categories and sub-categories.

rewrite url="~/Products/([a-zA-Z0-9]+).aspx" to="~/Products.aspx?Cat=$1"

rewrite url="~/Products/[a-zA-Z0-9]+)/[a-zA-Z0-9]+).aspx" to="~/Products.aspx?Cat=$1&SubCat=$2"
夏日浅笑〃 2024-10-15 21:10:20

尝试使您的正则表达式非贪婪:

rewrite url="~/Products/(.+?)/(.+).aspx" to="~/Products.aspx?Cat=$1&SubCat=$2"
                           ^

Try making your regex non-greedy as:

rewrite url="~/Products/(.+?)/(.+).aspx" to="~/Products.aspx?Cat=$1&SubCat=$2"
                           ^
梦年海沫深 2024-10-15 21:10:20

这是因为正则表达式匹配是贪婪的。第一行中的 (.+) 尽可能匹配,包括 xyz/abc。解决您的问题的快速方法可能是将第一个模式更改为使用 ([^/]+) (您可能必须根据您的环境以某种方式转义 / )。

That's because regular expression matching is greedy. The (.+) in the first line matches as much as it can, including xyz/abc. The quick fix for your problem is probably to change the first pattern to use ([^/]+) instead (you may have to escape the / somehow depending on your environment).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文