简体   繁体   中英

IIS URL Rewrite, Conditional change in URL

I've got a set of two IIS URL Rewrite rules, based on the "UserFriendlyURL" automatic rule. The point of these two rules is to take the first two querystring parameters (Type, and Series) and convert them to a user friendly URL. This page, if the user interacts with the search on it, may send you back to this page, but with other query string parameters, such as "page" or "pageSize".

I want those non-type/series parameters to stay as query string parameters, since they aren't part of the friendly URL, but if those items exist, I want the page to scroll down to the Search object (since it's obvious that the user just interacted with it).

To that end, I think what I want to do is add a Location Hash (#search) to the end. Rather than going through every search term on that page and manually adding "#search" to the end (many of them are actually onclick actions and I don't want to have to edit every JS method to add that to the end of generated URLs), it'd be nice to just capture the fact that any querystring parameter (other than the type/series) exists, and if so add "#search" to the end of the URL.

Is that doable, and if so, how? I know about Conditionals in regex: http://www.regular-expressions.info/conditional.html

But I don't think those would work in this case (I'd be happy to be wrong!)

<rewrite>
  <rules>
    <rule name="RedirectUserFriendlyURL1" stopProcessing="true">
      <match url="^(cn/)?/product-details$" />
      <conditions>
        <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
        <add input="{QUERY_STRING}" pattern="^Type=([^=&amp;]+)&amp;Series=([^=&amp;]+)$" />
      </conditions>
      <action type="Redirect" url="{R:1}/product-details/{C:1}/{C:2}" appendQueryString="false" />
    </rule>
    <rule name="RewriteUserFriendlyURL1" stopProcessing="true">
      <match url="^(cn/)?/product-details/([^/]+)/([^/]+)$" />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
      </conditions>
      <action type="Rewrite" url="{R:1}/product-details?Type={URLEncode:{R:2}}&amp;Series={URLEncode:{R:3}}" />
    </rule>
  </rules>
</rewrite>

So, the answer to this, as far as I can tell - Setting the hash like I described isn't possible with one, but with two, I can redirect ^Type=([^=&amp;]+)&amp;Series=([^=&amp;]+)&amp;(.+) to a different URL, {R:1}/product-details/{C:1}/{C:2}?{C:3}#search . AppendQueryString is false, here, because I'm already capturing the other query strings with &amp;(.+) => ?{C:3}

However, I also found an alternative solution, which is to just use Javascript/JQuery animate, detect the proper situation (in this case, check for other query strings) in JavaScript, and then have JS scroll to the proper anchor. This avoids needing to use IIS rewrite at all, and uses minimal javascript.

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