2016-09-16 90 views
0

我想爲我的web.config中的重定向規則寫一條規則。web.config中的IIS url重定向規則

我想重定向像URL ...

http://www.example.net/profile/username

到...

http://www.example.net/#/profile/username

我已經添加了以下運行,但它似乎並沒有工作。做這個的最好方式是什麼?

<rule name="Redirect to hashed profile" stopProcessing="true"> 
      <match url="(.*)" /> 
      <conditions> 
       <add input="{HTTP_HOST}{REQUEST_URI}" pattern="on" ignoreCase="true" pattern="^.net/profile/*"/> 
      </conditions> 
      <action type="Redirect" url="https://{HTTP_HOST}/#/{REQUEST_URI}" redirectType="Permanent" appendQueryString="true" /> 
     </rule> 

回答

1

以下是發生了什麼事以及如何修復它。

pattern="^.net/profile/*" 

^錨文本到你的HTTP_HOST是www.example.com的開頭,以便^.net意味着你的URL必須與任何1個字符.,然後字net開始。我要去承擔你的意思是你想匹配字面.,爲了做到這一點,你必須轉義爲\.

您還可以逃離/\/*在一個斜線結束只是意味着比賽無限的斜線,但你希望它匹配斜線無限次之後的任何字符,所以它的.*

讓我們重寫模式,因爲我們不能使用^,因爲你的網站不.net

啓動
pattern="\.net\/profile\/.*" 

這會給你這個..

<add input="{HTTP_HOST}{REQUEST_URI}" pattern="on" ignoreCase="true" pattern="\.net\/profile\/.*"> 

但是你真的需要{HTTP_HOST}?這真的取決於這是否是全球性規則,如果它不是全球性規則,並且它在應用程序級別。那你就不需要它了。

現在您可以重新加入^,將單詞profile固定到REQUEST_URI的第一部分。

<add input="{REQUEST_URI}" pattern="on" ignoreCase="true" pattern="^profile\/.*">