React-Router可讓您的ReactJS應用程序通過簡單的路由組件成爲SPA。部署到IIS時,使用bashHistory進行安裝時,一切正常。但是,當我嘗試使用browserHistory時,IIS正試圖返回一個目錄,而不是允許React-Router抓取和解析路徑。如何設置React-Router在IIS環境中工作?如何在IIS中設置react-router clean URL?
回答
讓React-Router與IIS一起工作的關鍵是設置URL重寫規則。看過其他人如何使用IIS安裝AngularJS SPA應用程序後,我提出了以下解決方案。
- 服務器(開發和生產)
設置規則趕上那不是文件,而不是目錄中的任何URL路徑上
下載並安裝URL Rewrite。或者,您可以否定任何您想要定期發送的實際目錄。更多信息可以在這裏找到在Microsoft's Documentation
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="ReactRouter Routes" stopProcessing="true"> <match url=".*" /> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> <add input="{REQUEST_URI}" pattern="^/(docs)" negate="true" /> </conditions> <action type="Rewrite" url="index.html" /> </rule> </rules> </rewrite> </system.webServer> </configuration>
在你的基地,html頁面(index.html的)添加
<base/>
標籤與href
屬性應用程式。<base href='/path/to/my/app'/>
只是建立在這個答案的人感興趣。如果你喜歡我,並需要通過腳本部署你的應用程序。以下是創建上述Web配置所必需的PowerShell腳本。
這會在服務器上安裝URL重寫。
#Install IIS UrlRewrite
If(Test-Path c:/msi) {
Invoke-WebRequest 'http://download.microsoft.com/download/C/F/F/CFF3A0B8-99D4-41A2-AE1A-496C08BEB904/WebPlatformInstaller_amd64_en-US.msi' -OutFile c:/msi/WebPlatformInstaller_amd64_en-US.msi
Start-Process 'c:/msi/WebPlatformInstaller_amd64_en-US.msi' '/qn' -PassThru | Wait-Process
cd 'C:/Program Files/Microsoft/Web Platform Installer'; .\WebpiCmd.exe /Install /Products:'UrlRewrite2,ARRv3_0' /AcceptEULA /Log:c:/msi/WebpiCmd.log
}
else {
New-Item c:/msi -ItemType Directory
Invoke-WebRequest 'http://download.microsoft.com/download/C/F/F/CFF3A0B8-99D4-41A2-AE1A-496C08BEB904/WebPlatformInstaller_amd64_en-US.msi' -OutFile c:/msi/WebPlatformInstaller_amd64_en-US.msi
Start-Process 'c:/msi/WebPlatformInstaller_amd64_en-US.msi' '/qn' -PassThru | Wait-Process
cd 'C:/Program Files/Microsoft/Web Platform Installer'; .\WebpiCmd.exe /Install /Products:'UrlRewrite2,ARRv3_0' /AcceptEULA /Log:c:/msi/WebpiCmd.log
}
這裏是你如何添加規則。
Add-WebConfigurationProperty -pspath "$IISPath\MyWebsite" -filter "system.webServer/rewrite/rules" -name "." -value @{name='Home';stopProcessing='True'}
Set-WebConfigurationProperty -pspath "$IISPath\MyWebsite" -filter "system.webServer/rewrite/rules/rule/match" -name "url" -value ".*"
Add-WebConfiguration -pspath "$IISPath\MyWebsite" -filter "system.webServer/rewrite/rules/rule[@name='Home']/conditions" -value @{ input="{REQUEST_FILENAME}"; matchType="IsFile"; negate="true" }
Add-WebConfiguration -pspath "$IISPath\MyWebsite" -filter "system.webServer/rewrite/rules/rule[@name='Home']/conditions" -value @{ input="{REQUEST_FILENAME}"; matchType="IsDirectory"; negate="true" }
Add-WebConfiguration -pspath "$IISPath\MyWebsite" -filter "system.webServer/rewrite/rules/rule[@name='Home']/conditions" -value @{ input="{REQUEST_URI}"; pattern="^/(docs)"; negate="true" }
Set-WebConfigurationProperty -pspath "$IISPath\MyWebsite" -filter "system.webServer/rewrite/rules/rule/action" -name "type" -value "Rewrite"
Set-WebConfigurationProperty -pspath "$IISPath\MyWebsite" -filter "system.webServer/rewrite/rules/rule/action" -name "url" -value "index.html"
這些腳本可以通過Powershell中的Invoke-Command在本地或遠程執行。
另外我在使用React Router警告和在index.html的標題中使用基本標記時遇到了一些麻煩。這個鏈接相當有幫助。
https://github.com/ReactTraining/react-router/issues/4006
我基本上是我的陣營,路由器的封裝和歷史包更改爲版本3.x和設置配置在我的路由器擺脫廢棄警告的。這是我現在的反應路由器。
import { useRouterHistory } from 'react-router'
import { createHistory } from 'history'
const history = useRouterHistory(createHistory)({
basename: '/base-path'
})
ReactDOM.render(
<Router history={history}>
....
- 1. 在IIS中設置URL重定向7
- 2. 如何設置IIS?
- 3. 如何爲IIS中的單個URL設置IP白名單
- 4. Clean URL&Image Upload
- 5. 如何在沒有IIS管理器的情況下設置IIS 7.0 URL重寫?
- 6. PyroCMS clean URL問題
- 7. 如何在IIS上設置Mercurial和hgwebdir?
- 8. 如何在codeigniter中設置基址url
- 9. 如何在Ember中設置根URL
- 10. 如何在IIS中設置條件UrlRewrite以檢查URL是否有效
- 11. Web.config如何繼承IIS配置設置?
- 12. 如何做iis 7.0 CLEAN在vista上重新安裝?
- 13. 如何在IIS中重寫URL?
- 14. 在IIS 7.5中設置默認WebPage
- 15. IIS 7.5設置
- 16. 如何在Visual Studio 2010中進行調試時設置IIS?
- 17. 如何在ColdFusion無響應時在IIS 6中設置超時
- 18. Clean URL無效,安裝了mod_rewrite模塊
- 19. 如何設置,並從URL
- 20. 如何設置NSPathComponentCell的URL?
- 21. 如何設置API URL
- 22. 如何設置Mongo URL?
- 23. HTML5畫布 - origin-clean標誌如何設置爲false?
- 24. 在IIS Express上設置SMTP
- 25. 如何使用ssl證書設置iis?
- 26. 如何使用IIS設置請求頭
- 27. 如何設置子域上的IIS 7
- 28. ReactRouter和Flux(altjs)
- 29. 如何設置cookie的url中jQuery的
- 30. 如何設置magento中的基礎URL?
非常感謝。在IIS上沒有很多React'ers。這非常有幫助。投票! – Puerto
很高興你發現它有幫助! IIS不是我的第一選擇,但必要性是所有發明的根源:) – jOshT
當您執行此更改時,您是否看到過與您的反應路由器相關的任何錯誤? 「警告:使用 自動設置基本名稱已被棄用,並將在下一個主要版本中刪除。 的語義與基本名稱有細微的差異,請將選項中的基本名稱顯式傳遞給createHistory」 –
Puerto