Regex to extract a part out of a URL and use it as

2019-05-22 18:17发布

问题:

OK, I'm wondering if someone can lend a hand with a regex I'm trying to write.

Basically, what I want to do is use IIS urlrewrite module to do a redirect to a specific URL if the user accesses a URL on another site. The only catch is I have to also capture a bit of the query string, and move it into the redirect. I think I actually DO have the regex correct for this, but it's implementing it in my web.config that is causing the problem.

so here is the input, the URL that a user may access would look like:

https://of.example.com/sfsv3.aspx?waform=pro&language=en

I want to match that URL (either http or https, case insensitive), and capture from it also one piece of information, the two letter language code. then the url i want to forward the user to looks like:

http://example.com/ca/en/ppf

(where en is replaced by whatever i captured above)

So, I'm working with IIS Rewrite module, and I've gotten my input data and regex in, so far the regex pattern I have is this:

https?://of.example.com/sfsv3.aspx\?waform=pro&(language=(..))

so basically i'm matching the whole string, plus a group and a subgroup for language and it's code. in the IIS test pattern dialog, this is working.

I get the following

{R:1} language=en

{R:2} en

great! so then my IIS rewrite rule should look like this to redirect the user:

<system.webServer>
    <rewrite>
      <rules>
        <rule name="test" stopProcessing="true" enabled="true">
          <match url="https?://of.example.com/sfsv3.aspx\?waform=pro&(language=(..))" ignoreCase="true" />
          <action type="Redirect" url="http://www.example.com/ca/{R:2}/ppf" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

this all seems right to me. however, IIS is balking and saying that my web.config is invalid. specifically, it says "Entity '(' is not defined" and it's pointing to the ( right before language as the problem. So, I can't build or deploy this application till I fix that. If i build it without and then just try to drop that into the web.config, i get an error loading the site:

HTTP Error 500.19 - Internal Server Error The requested page cannot be accessed because the related configuration data for the page is invalid.

Can someone help me figure out how to put a capture group into a rewrite rule properly?

回答1:

You forgot to code the ampersand. Should be:

<system.webServer>
<rewrite>
  <rules>
    <rule name="test" stopProcessing="true" enabled="true">
      <match url="https?://of.example.com/sfsv3.aspx\?waform=pro&amp;(language=(..))" ignoreCase="true" />
      <action type="Redirect" url="http://www.example.com/ca/{R:2}/ppf" />
    </rule>
  </rules>
</rewrite>