Flutter网页刷新出现404 Not Found错误

问题描述 投票:0回答:4

在主要方法中,我使用了 UrlStrategy 包,如下所示,因为我不想在我的 url 中使用 #:

void main() {
  setPathUrlStrategy();
  runApp(const MyApp());
}

我在调试模式下测试网站时,没有问题。然后我发布了我的网站。主页就像www.example.com,当我尝试用F5刷新页面时,没有问题。然后我导航到另一个页面,例如 www.example.com/about-us 并尝试再次使用 F5 刷新页面,我收到“404 not found”错误。

为了解决这个问题,我在 htdocs 中创建了 .htaccess 文件并在该文件中写入:

RewriteEngine on
RewritwCond %{REQUEST_FILENAME} !-d
RewritwCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.html [L]

但是我开始收到 500 内部服务器错误。

htdocs 之外存在一个现有的 .htaccess 文件。我复制了上面的代码并将其粘贴到现有的 .htaccess 中。但又出现同样的问题。现有.htaccess文件中的代码如下:

##################################################
#
# DO NOT EDIT THIS FILE
#
# Create a new .htaccess file in your htdocs
# directory (or example.com/htdocs/ directory)
# to add your own rules or override these rules.
#
##################################################

DirectoryIndex index.php index.html index.htm index2.html

ErrorDocument 403 https://infinityfree.net/errors/403/
ErrorDocument 404 https://infinityfree.net/errors/404/
ErrorDocument 500 https://infinityfree.net/errors/500/

我必须做什么?

flutter routes http-status-code-404 refresh
4个回答
7
投票

我在 htdocs 中创建了一个名为 .htaccess 的文件。我粘贴了下面给出的代码:

RewriteEngine On
RewriteBase /
RewriteCond $1 !^(index\.html|assets|robots\.txt|favicon\.png)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.html [L]

问题解决了。


1
投票

如果您使用 nginx 而不是 apache,请使用下面的代码。

location / {
  if (!-e $request_filename){
    rewrite ^(.*)$ /index.html break;
  }
}

0
投票

将其添加到 /etc/nginx/conf.d/ 中的 nginxconf 中

location / {
    try_files $uri $uri/ /index.html =404;
}

并删除或注释掉

try_files $uri $uri/ /index.php?$args;


-2
投票

根据 Mustafa Deniz 的回答,这是 web.config 版本:

感谢 ChatGPT。

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Rewrite to index.html">
          <match url="^(.*)$" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_URI}" negate="true" pattern="^/index\.html$" />
            <add input="{REQUEST_URI}" negate="true" pattern="^/assets" />
            <add input="{REQUEST_URI}" negate="true" pattern="^/robots\.txt$" />
            <add input="{REQUEST_URI}" negate="true" pattern="^/favicon\.png$" />
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="/index.html" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>
© www.soinside.com 2019 - 2024. All rights reserved.