正则表达式与可选的查询字符串参数

问题描述:

我有这样的重写规则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> 

该URL的工作原理:http://www.example.com/apartment/rent/province/texas/street/houston/mystreet
但是当我添加查询字符串参数,该URL会抛出404:http://www.example.com/apartment/rent/province/texas/street/houston/mystreet?rooms=3&pricemin=2500

我已经在此处检查过:
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"属性解决这个问题,但这显然不起作用。

我怎样才能确保我的重写规则也适用于查询字符串参数?

当我看着你的规则,我知道你正在寻找URL路径完全吻合(^...$)。

但是{UNENCODED_URL}may contain query字符串也。因此,当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> 
+0

Jup,就是这样。谢谢,将在23小时内奖励赏金:-) – Flo

+0

@Flo欢迎您:) –