开发者

How to ignore "?" in a regular expression?

I've been using regular expressions in IIS to do a redirect so that if someone visits /test01/ it will redirect them to /test.asp?kw=test01. However, I still want to be able to append a querystring, so that /test01/src=url would redirect to /test.asp?kw=test01&src=url.

This was easy enough to do, but what I would like i开发者_StackOverflows so that they could either use /test01/src=url OR /test01/?src=url, so the regex would be smart enough to ignore the question mark.

I've been using: ^test01/(.*) to redirect to /test.asp?kw=test01&{R:1}. I have tried ^test01/(\??)(.*) under the understanding that question marks allow the preceding character to be ignored, but that did not work, and I'm not exactly sure how to approach this. Any advice would be appreciated.


Try:

^test01/\??(.*)

Otherwise, your \?? is backreference #1, and your querystring becomes backreference #2.


Update

It also may be that you need to disable back-tracking, using yet another meaning of +:

^test01/\??+(.*)

I don't know whether IIS supports this, though. And PHP's engine doesn't seem to need it.


The following expression should work.

  ^test01/[?]?(.*)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜