2017-09-14 87 views
2

我有這樣的重寫規則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"屬性解決這個問題,但這顯然不起作用。

我怎樣才能確保我的重寫規則也適用於查詢字符串參數?

回答

1

當我看着你的規則,我知道你正在尋找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歡迎您:) –

相關問題