gpt4 book ai didi

apache - 托管在 apache2 上的 Vue 路由器

转载 作者:搜寻专家 更新时间:2023-10-30 22:10:04 25 4
gpt4 key购买 nike

我在 apache 服务器上托管我的 vuejs 项目。

  1. 初始化项目
  2. 设置路由器
  3. 构建并托管到 apache 服务器

const router = new VueRouter({
routes: Routes,
// relative: true,
mode: 'history'
});

这在本地运行良好,但 vue 路由器在服务器上中断。示例

如果我运行 http://example.com然后转到 http://exmaple.com/sub效果很好

但是如果我刷新页面,它会抛出 404 错误。

错误: enter image description here

最佳答案

来自vue.js documentation page :

When using history mode, the URL will look "normal," e.g. http://oursite.com/user/id. Beautiful!

Here comes a problem, though: Since our app is a single page client side app, without a proper server configuration, the users will get a 404 error if they access http://oursite.com/user/id directly in their browser. Now that's ugly.

Not to worry: To fix the issue, all you need to do is add a simple catch-all fallback route to your server. If the URL doesn't match any static assets, it should serve the same index.html page that your app lives in. Beautiful, again!

Apache

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

关于apache - 托管在 apache2 上的 Vue 路由器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47879936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com