部署的 vuejs vue-router 应用程序上页面刷新返回 404 错误

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

应用程序的主页/基本网址可以毫无问题地刷新。但其他页面在页面刷新时返回 404。请问有什么解决办法吗?

这是 404 的屏幕截图。 [1]:https://i.stack.imgur.com/lc8xD.png

vue.js vue-router vuejs3
5个回答
4
投票

发生这种情况是因为 Vue 路由仅在前端。因此,当您刷新页面时,它会转到后端服务器。它会检查是否有任何文件来满足请求。

解决这个问题。您必须告诉您的 apache Web 服务器为您的前端 Vue 应用程序处理它。


2
投票

对我有用的解决方案:

/etc/apache2/sites-available/"yourAppDomain".conf 添加到末尾:

后备资源/index.html

</VirtualHost>


1
投票

尝试将其添加到您的

.htaccess

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.html [L]
</IfModule>

0
投票

页面刷新返回 404 错误 VueJs (mode:history) 和 Laravel:

适用于 Laravel 的解决方案:

假设您的入口点是index方法,您只需将此路由添加到routes/web.php文件的底部即可。

Route::get('{any}', function () {
    return view('welcome');
})->where('any','.*');

0
投票

我找到了适合我的解决方案,路由器也适用:)

IIS 设置重写规则:

<rule name="MY SPA APP REWRITE RULE" stopProcessing="true">
    <match url="myAppBaseURL/(.*)" />
    <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="/myAppBaseURL?rewriteURL={R:1}" />
</rule>

然后在主App.vue中添加这个

import { useRouter } from 'vue-router'
const router = useRouter()

if(new URLSearchParams(window.location.search).get('rewriteURL')){
  router.push(new URLSearchParams(window.location.search).get('rewriteURL'))
}
© www.soinside.com 2019 - 2024. All rights reserved.