在主要方法中,我使用了 UrlStrategy 包,如下所示,因为我不想在我的网址中包含 #:
void main() {
setPathUrlStrategy();
runApp(const MyApp());
}
Run Code Online (Sandbox Code Playgroud)
我在调试模式下测试网站时,没有问题。然后我发布了我的网站。主页就像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]
Run Code Online (Sandbox Code Playgroud)
但我开始收到 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.
# …Run Code Online (Sandbox Code Playgroud)