2017-05-15 111 views
1

我已將reactjs應用程序與react路由器配合使用。我不想使用散列歷史,我遇到刷新頁面創建404錯誤(反應路由器刷新時不加載和瀏覽器想要獲取不存在的內容,因此404)加載問題。我找到了一些解決方案,並且我想將每個請求重定向到根,因此服務器總是首先使用導入標籤獲取index.html。IIS重定向根目錄上的所有請求

我的文件夾結構很簡單:

ROOT/ 
-- index.html 
-- static/ 
    -- js/ 
     -- main.js 
-- css/ 
etc... 

我對這個網站上發現這樣的web.config規則:

<rewrite> 
    <rules> 
     <rule name="redirect all requests" stopProcessing="true"> 
      <match url="^(.*)$" ignoreCase="false" /> 
      <conditions logicalGrouping="MatchAll"> 
       <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" pattern="" ignoreCase="false" /> 
      </conditions> 
      <action type="Rewrite" url="index.html" appendQueryString="true" /> 
     </rule> 
    </rules> 
</rewrite> 

這工作很細跟第一個文件夾路徑上的網址,我的意思是所有像/items/contact的請求都不錯。但是/items/detail之類的請求已被破壞,因爲瀏覽器正在查看items文件夾,而不是文檔根目錄。另外非常重要的是我的webpack以這種方式生成index.html腳本標籤:<script type="text/javascript" src="./static/js/main.js">

src屬性有可能是因爲那個./?我怎麼能告訴服務器(IIS 10)「嘿,別看別的地方,但進入文檔根目錄?」

謝謝你們。

回答

2

這是我發現的解決方案,我有一些路徑,我仍然想通過我的IIS來處理,但其餘的我想反應照顧。

<configuration> 
     <system.webServer> 
      <rewrite> 
       <rewriteMaps> 
        <rewriteMap name="^(.*)$" /> 
       </rewriteMaps> 
       <rules> 
        <rule 
         name="redirect all requests" 
         stopProcessing="true" 
        > 
         <match url="^(.*)$" /> 
         <conditions logicalGrouping="MatchAll"> 
          <add 
           input="{REQUEST_URI}" 
           pattern="/someOtherResourceIstillWantIIStoHandle(.*)$" 
           negate="true" 
          /> 

          <add 
           input="{REQUEST_FILENAME}" 
           matchType="IsFile" 
           negate="true" 
          /> 
          <add 
           input="{REQUEST_URI}" 
           pattern="/resourcecentre(.*)$" 
           negate="true" 
          /> 
         </conditions> 
         <action type="Rewrite" url="/index.html" /> 
        </rule> 
       </rules> 
      </rewrite> 
     </system.webServer> 
    </configuration> 
相關問題