2017-05-15 105 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> 
相关问题