【问题标题】:Regex for fixing URL patterns用于修复 URL 模式的正则表达式
【发布时间】:2015-06-09 20:27:31
【问题描述】:

我有以下 url 结构:

http://www.xyxyxyxyx.com/ShowProduct.aspx?ID=334
http://www.xyxyxyxyx.com/ShowProduct.aspx?ID=1094

等等..

最近我用IIS rewrite把这个结构重写为

http://www.xyxyxyxyx.com/productcategory/334/my-product-url
http://www.xyxyxyxyx.com/productcategory/1094/some-other-product-url

等等..

这很好用。

我想创建另一个规则,这样如果一个无效的 url 请求带有以下结构:

http://www.xyxyxyxyx.com/productcategory/ShowProduct.aspx?ID=334

'productcategory' 部分应该从 url 中删除,并且 url 应该看起来像

http://www.xyxyxyxyx.com/ShowProduct.aspx?ID=334

我该如何编写这条规则?

【问题讨论】:

    标签: asp.net regex url-rewriting


    【解决方案1】:

    它可能会根据您用于应用正则表达式的方式而有所不同,但这是一个基本的:

    's|productcatgory/||'
    

    如果你想确保它也只在 xyxyxyxyx url 存在时这样做,这应该可以工作:

    's|^http://www\.xyxyxyxyx\.com/productcategory/|http://www\.xyxyxyxyx\.com/|'
    

    编辑:啊,所以如果 productcategory 可以是任何类别,那么您需要围绕它进行匹配,如下所示:

    's|^http://www\.xyxyxyxyx\.com/.*/ShowProduct|http://www\.xyxyxyxyx\.com/ShowProduct|'
    

    【讨论】:

    • 谢谢 Drake,这里的 'productcategory' 可以是汽车、踏板车、自行车等任何东西,所以我如何让它在 'xyxyxyxyx.com' 和 'ShowProduct.aspx?ID= 334'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多