views:

152

answers:

1

This might be more a a regex question, but it is holding up our release. I'm unable to come up with a clever solution.

Basically, we want to rewrite www.oursite.com/Games.aspx?g=classic&m=etc to www.oursite.com/classic/?m=etc

The problem is that the rule itself (at least as generated by the URL Rewrite mod for IIS7) looks like this:

<rewrite>
      <rules>
        <rule name="RedirectUserFriendlyURL1" stopProcessing="true">
          <match url="^Games\.aspx$" />
          <conditions>
            <add input="{REQUEST_METHOD}" negate="true" pattern="^POST$" />
            <add input="{QUERY_STRING}" pattern="^g=([^=&amp;]+)$" />
          </conditions>
          <action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent" />
        </rule>
        <rule name="RewriteUserFriendlyURL2" stopProcessing="true">
          <match url="([^/]+)/?$" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="Games.aspx?g={R:1}" />
        </rule>
      </rules>
    </rewrite>

And thus, any other file that matches a similar pattern, for example Item/?id=23 is being rewritten. In addition, our script resource file is being rewritten, so the whole site throws 30 javascript errors. According to our specs, it's unacceptable in our specs to have the url www.oursite.com/Games/classic OR www.oursite.com/g/classic. Any solution? Manythanks!

+2  A: 

Perhaps instead of matching ([^/]+)/?$ in your second rule, you'd be better of explicitly stating which things you want to rewrite, in a regex OR statement:

(classic|somethingelse|athirdsomething)/?$

That way only the items you explicitly want to rewrite will be modified. I don't know how many of these you're potentially needed to rewrite, so if it's a large number, this might not be viable, but if it's only a few categories, then it's probably the most straightforward.

Alternatively, if there are only certain prefixes that shouldn't be rewritten, you could simply add negated conditions that pattern match those prefixes to your rewrite.

Amber
Thanks, Dav - we thought about this, but there will be an unknown number of items and they need to be able to change.
Vibhu