问题
I have a single page React app hosted in Azure blob storage but am getting an The requested content does not exist. error when deep linking into a page:
I've enabled static website option in the storage account:
The files are all in place in the $web container:
This includes the following web.config with a rewrite rule that is supposed to let index.html handle all the routing:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="React Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Can anyone see where I've gone wrong?
回答1:
The static website in Azure Storage GPv2 is different from Azure App Service. It only hosts these static web files which include HTML/CSS/JavaScript files, other files can be handled in browser like images, robots.txt
, etc. It has inability to process server-side scripts, due to there is not IIS. So your web.config
file is no sense for it to change the access routing and be belong to server-side script for IIS.
Actually, you can see the words in Azure portal.
Configuring the blob service for static website hosting enables you to host static content in your storage account. Webpages may include static content and client-side scripts. Server-side scripting is not supported in Azure Storage. Learn more
And refer to the Learn more
link of Static website hosting in Azure Storage
In contrast to static website hosting, dynamic sites that depend on server-side code are best hosted using Azure App Service.
I recommended using Azure App Service for your app if requires the URL-rewrite feature.
回答2:
You can do the deep linking if you point the 404 page back at your index.html
. This is not a perfect solution - has side effects - but it will work for the majority of cases.
回答3:
None of these solutions worked for me, and I miserably started to accept that I wouldn't be able to host my Angular application (with Routing) just in Azure Storage.
But, actually, I found a solution.
You need to manually add a web.config file to your src folder (yes, even though we're not hosting this on an IIS server)
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Main Rule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
...then add this file to the assets
section in your angular.json file:
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
. . .
"assets": [
"src/web.config"
],
Shockingly, this works now.
In my Angular app, I could click around to various sub-pages, eg
http://www.mikesapp.web.core.windows.net/Orders/List
and, crucially, I can also open a browser and go directly to this webpage.
来源:https://stackoverflow.com/questions/55598531/web-config-rewrite-rule-not-working-in-azure-static-website-in-blob-storage