部署到服务器404后会出现Flutter Web我知道flutter是单页应用程序所以我们配置.htaccess文件
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}% !-d
RewriteCond %{REQUEST_FILENAME}% !-f
RewriteRule . /index.html [L]
但我在应用程序中使用一个静态 html 文件渲染,因此此方法不起作用并且我也尝试了 404.html。没用请帮帮我
RewriteCond %{REQUEST_FILENAME}% !-d RewriteCond %{REQUEST_FILENAME}% !-f
您在
TestString参数末尾有一个错误的文字
%
,因此这些 neated 条件将始终成功,包括当请求重写为 /index.html
时。
这些条件应写成:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
但请注意,为了正确重写对文档根目录的请求,您需要确保正确设置
DirectoryIndex
。例如:
DirectoryIndex index.html
我在 Windows 服务器 IIS 上发布了我的 flutter web 版本,但在重新加载时出现错误。 对于重新加载 flutter 页面时出现的 404 错误,我在发布的根目录中的 index.html 旁边添加了一个 web.config 文件,并在其中添加了此代码。
<?xml version="1.0" encoding="UTF-8"?>
<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>
参考 MrWhite 的 answer 和 johannesmilke 的视频
在“./myApp/web/.htaccess”下创建文件
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.html [L]
似乎可以解决问题。而且这样做似乎也将包含在构建中。