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

Regex for fixing URL patterns

我有以下 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

这条规则怎么写?

它可能会有所不同,具体取决于您用来应用正则表达式的内容,但这是一个基本的:

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