IIS URL Rewrite, Conditional change in URL

时间:2016-03-04 18:20:08

标签: regex iis url-rewriting iis-7 url-rewrite-module

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>

1 个答案:

答案 0 :(得分:2)

所以,据我所知,答案是这样的 - 像我描述的那样设置哈希是不可能的,但有两个,我可以将^Type=([^=&amp;]+)&amp;Series=([^=&amp;]+)&amp;(.+)重定向到不同的网址{R:1}/product-details/{C:1}/{C:2}?{C:3}#search。这里,AppendQueryString为false,因为我已经使用&amp;(.+) =&gt;捕获了其他查询字符串。 ?{C:3}

但是,我还找到了另一种解决方案,即只使用Javascript / JQuery动画,在JavaScript中检测正确的情况(在这种情况下,检查其他查询字符串),然后让JS滚动到正确的锚点。这样就可以完全避免使用IIS重写,并使用最少的javascript。