使用可选查询字符串参数重写URL的正则表达式 [英] Regex for URL rewrite with optional query string parameters

查看:166
本文介绍了使用可选查询字符串参数重写URL的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个重写规则:

<rule name="rentals by proptype+state+city+street test" stopProcessing="true">
  <match url=".*" />
  <conditions>
    <add input="{UNENCODED_URL}" pattern="^/([a-zA-Z0-9\-+]+)/rent/province/([a-zA-Z\-+]+)/street/([a-zA-Z0-9%\-+]+)/([0-9a-zA-Z%\-+']+)$" />
  </conditions>
  <action type="Rewrite" url="search_new.aspx?proptype={C:1}&amp;province={C:2}&amp;city={C:3}&amp;street={C:4}" appendQueryString="true" />
</rule>

我也尝试过:

<rule name="rentals by proptype+state+city+street test" stopProcessing="true">
  <match url=".*" />
  <conditions>
    <add input="{UNENCODED_URL}" pattern="^/([a-zA-Z0-9\-+]+)/rent/province/([a-zA-Z\-+]+)/street/([a-zA-Z0-9%\-+]+)/([0-9a-zA-Z%\-+']+)$" />
    <add input="{QUERY_STRING}" pattern=".*" />
  </conditions>
  <action type="Rewrite" url="search_new.aspx?proptype={C:1}&amp;province={C:2}&amp;city={C:3}&amp;street={C:4}" appendQueryString="true" />
</rule>

此网址有效: http://www.example .com/apartment/rent/province/texas/street/houston/mystreet
但是,当我添加查询字符串参数时,URL会抛出404:

This URL works: http://www.example.com/apartment/rent/province/texas/street/houston/mystreet
But when I add query string parameters, the URL throws a 404: http://www.example.com/apartment/rent/province/texas/street/houston/mystreet?rooms=3&pricemin=2500

我已经在这里检查:
IIS URL重写不适用于查询字符串
https://docs.microsoft.com/zh-CN/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference
https://msdn.microsoft.com/en-us/library/ms972974. aspx

I already checked here:
IIS URL Rewrite not working with query string
https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference
https://msdn.microsoft.com/en-us/library/ms972974.aspx

似乎我必须使用QUERY_STRING服务器变量. 我实际上只想附加查询字符串参数,而不必为每个参数编写特殊的映射.我以为可以通过appendQueryString="true"属性解决此问题,但这显然行不通.

It seems I have to use a QUERY_STRING server variable. I actually just want to append the query string parameters, without having to write a special mapping for each parameter. I thought I could solve this through the appendQueryString="true" property, but that apparently doesn't work.

如何确保我的重写规则也可以与查询字符串参数一起使用?

How can I make sure my rewrite rule works also with query string parameters?

推荐答案

当我查看您的规则时,我了解到您正在寻找与 URL路径的完全匹配(^...$).

When I look at your rule, I understand that you are looking for a complete match (^...$) with URL Path.

但是{UNENCODED_URL} 可能包含查询字符串.因此,即使URL只是查询分隔符(?),当URL包含任何查询字符串时,这也会破坏您的规则.

However {UNENCODED_URL} may contain query strings too. So this breaks your rule when the URL contains any query string, even if it's just a query separator (?).

要解决此问题,您应该查找匹配项,直到查询字符串的开头,而不是结尾.

To fix this you should look for a match until the beginning of the query string instead, not till the end.

尝试以下规则.

<rule name="rentals by proptype+state+city+street test" stopProcessing="true">
    <match url=".*" />
    <conditions>
        <add input="{UNENCODED_URL}" pattern="^/([a-zA-Z0-9\-+]+)/rent/province/([a-zA-Z\-+]+)/street/([a-zA-Z0-9%\-+]+)/([0-9a-zA-Z%\-+']+)" />
    </conditions>
    <action type="Rewrite" url="search_new.aspx?proptype={C:1}&amp;province={C:2}&amp;city={C:3}&amp;street={C:4}" appendQueryString="true" />
</rule>

这篇关于使用可选查询字符串参数重写URL的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆