2010-11-17 40 views
0

我正在使用以下web.config文件將站點的非www版本重定向到www版本。不過,我也想讓它剝離索引文件的文件名稱。Windows Server Web.config條索引文件名

例如: 重定向到www.example.com/index.html www.example.com

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true" /> 
     <rewrite> 
      <rules> 
       <rule name="CanonicalHostNameRule" stopProcessing="true"> 
        <match url="(.*)" /> 
        <conditions> 
         <add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" /> 
        </conditions> 
        <action type="Redirect" url="http://www.example.com/{R:1}" /> 
       </rule> 
      </rules> 
     </rewrite> 
    </system.webServer> 
</configuration> 

編輯:

這裏是我的更新配置文件。但是,現在它造成了500錯誤。

如下:)

回答

2

見CodingGorilla的回答爲重定向後襬脫index.html的,下降的{R:1}。但是,那麼您需要修改該規則,以便爲/index.html請求觸發僅觸發,並創建一個新規則,觸發其他頁面(包括{R:1}),以便example.com/mypage.html的請求仍會得到重定向正確。

編輯:

編輯#2

,最後的答案是!

基於我們的聊天對話,我認爲這是最終的規則集:

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true" /> 
     <rewrite> 
      <rules> 
       <rule name="CanonicalHostNameRule1" stopProcessing="true"> 
        <match url="index\.htm(?:l)?" /> 
         <conditions> 
          <add input="{HTTP_HOST}" pattern="example\.com$" /> 
         </conditions> 
         <action type="Redirect" url="http://www.example.com/" /> 
       </rule> 
       <rule name="CanonicalHostNameRule2" stopProcessing="true"> 
        <match url="(.*)" /> 
        <conditions> 
         <add input="{HTTP_HOST}" pattern="^example\.com$" /> 
        </conditions> 
        <action type="Redirect" url="http://www.example.com/{R:1}" /> 
       </rule> 
      </rules> 
     </rewrite> 
    </system.webServer> 
</configuration> 
+0

太好了!我怎麼做?哈 – Batfan 2010-11-18 18:27:17