React 使用browserHistory項目訪問404問題


最近項目里面用到了React但是發布到iis站點之后,路由地址 刷新訪問直接404錯誤。查閱資料之后發現是iis缺少配置URL重寫 的問題導致的。下面我們來圖形化配置,簡單的配置下IIS

打開IIS使用 Web平台安裝程序

搜索url關鍵字,您會看到

React-Router-browserHistory-IIS-2018518104435
直接安裝

關掉IIS 重新打開IIS在站點右邊的控制面板可以看到一個URL重寫的功能

新增配置如下

React-Router-browserHistory-IIS-2018518104817

也可以直接 使用我的配置

配置如下 關鍵節點是:rewrite

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
       <rewrite>
            <rules>
                <rule name="ReactRouter" patternSyntax="ECMAScript" stopProcessing="true">
                    <match url=".*" />
                    <conditions>
						<add input="{HTTP_METHOD}" pattern="^GET$" />
						<add input="{HTTP_ACCEPT}" pattern="^text/html" />
						<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
					</conditions>
					<action type="Rewrite" url="/index.html" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

附:其他http-server配置說明

Nginx

server {
	server_name react.yahui.wang
	listen 80;

	root /wwwroot/ReactDemo/dist;
	index index.html;
	location / {
    	try_files $uri /index.html;
  	}
}

Tomcat

找到conf目錄下的web.xml文件,然后加上一句話讓他定位回來

<error-page>
    <error-code>404</error-code>
    <location>/index.html</location>
</error-page>

Apache

.htaccess文件配置如下

<IfModule mod_rewrite.c>

  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  RewriteRule . /index.html [L]

</IfModule>

轉載請注明出處 http://blog.yahui.wang/2018/05/18/React-Router-browserHistory-IIS/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM