1

I have a rewrite rule in my web.config file like this:

  <rule name="Public page" stopProcessing="true">
      <match url="^([^/]+)/([^/]+)/?$" />
      <conditions>
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          <add input="{URL}" pattern="\.(.*)$" negate="true" />
      </conditions>
      <action type="Rewrite" url="public/place/{R:2}.aspx?cname={R:1}" />
    </rule>

What it does is let the client type in a dynamic place name and then the page he wants to see. e.g., I have a page that displays news for companies. I want to see news for a company named "Pavilion" so I type:

http://localhost/pavilion/news/ 

and the URL rewrite takes me to

http://localhost/public/news.aspx?cname=pavilion

That works. But I'm getting a lot of errors about page not found for case like this: An image's src attribute that points to some Non Existing name from another page. e.g., in products page (http://localhost/products/showproducts.aspx) I have:

<img src='undefined'/>

when I debug I see that it fires the rule thinking that products is {R:2} and undefined is {R:1} so it tries to load http://localhost/products/undefined/ and I get the error: The file '/public/undefined.aspx' does not exist.

I would like the rule not to fire if the first segment of the url is directory or file. Does any one has an idea how can I do such a check?

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
jekcom
  • 2,065
  • 2
  • 24
  • 34

1 Answers1

0

The answer is to use the back-reference in condition input string.

 <rule name="Public page" stopProcessing="true">
                <match url="^([^/]+)/([^/]+)/?$" />
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    <add input="{URL}" pattern="\.(.*)$" negate="true" />
                    <add input="{DOCUMENT_ROOT}\{R:1}" matchType="IsFile" negate="true" />
                    <add input="{DOCUMENT_ROOT}\{R:1}" matchType="IsDirectory" negate="true" />
                </conditions>
                <action type="Rewrite" url="public/place/{R:2}.aspx?cname={R:1}" />
            </rule> 

Thanks to Leo for the answer

jekcom
  • 2,065
  • 2
  • 24
  • 34