简体   繁体   中英

IIS7 URL Rewrite Mod causes unwanted effects

This might be more aa 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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM