2017-06-15 83 views
0

我需要捕獲非文件夾的文件夾請求,以便將請求的路徑作爲查詢字符串重定向到默認頁面。將IIS URL重寫爲查詢字符串參數

例如,如果用戶請求

www.mysite.com/IT 

我希望它重定向到

www.mysite.com/default.aspx?q=IT 

但是,如果他們請求一個網頁,在真正子文件夾,該不應該被重定向。例如,對www.mysite.com/projects/new.aspx的請求不應嘗試執行任何重定向,因爲它是網站中的物理文件夾和文件。

回答

1

使用IIS Rewrite module和設置它在web.config中:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <system.webServer> 
     <rewrite> 
      <rules> 
       <clear /> 
       <rule name="Redirector" patternSyntax="Wildcard"> 
        <match url="*" /> 
        <conditions> 
         <add input="{PATH_INFO}" pattern="/*" /> 
         <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
         <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
        </conditions> 
        <action type="Rewrite" url="/default.aspx?q={PATH_INFO}" appendQueryString="true" /> 
       </rule> 
      </rules> 
     </rewrite> 
    </system.webServer> 
</configuration> 
+1

太棒了!那樣做了 –

0

你可以定義404錯誤頁面,會重定向到您的默認頁的「Q =」你的願望

相關問題