用于修复URL模式的正则表达式

时间:2015-06-08 03:14:52

标签: asp.net regex url-rewriting

我有以下网址结构:

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

依旧......

最近我使用IIS重写将此结构重写为

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'部分应该从网址中删除,网址应该看起来像

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

如何编写此规则?

1 个答案:

答案 0 :(得分:0)

根据您使用正则表达式所使用的内容,它可能会有所不同,但这是一个基本的:

'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|'