【问题标题】:Vue Router hosting on apache2Vue 路由器托管在 apache2 上
【发布时间】:2018-06-01 11:21:21
【问题描述】:

我在 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 错误。

错误:

【问题讨论】:

标签: apache vue.js vuejs2 vue-router


【解决方案1】:

来自vue.js documentation page:

使用历史模式时,URL 看起来“正常”,例如http://oursite.com/user/id。漂亮!

但问题来了:由于我们的应用程序是单页客户端应用程序,没有适当的服务器配置,如果用户直接在浏览器中访问http://oursite.com/user/id,将会收到 404 错误。现在这很难看。

不用担心:要解决此问题,您只需向服务器添加一个简单的全能后备路由即可。如果 URL 不匹配任何静态资源,它应该提供与您的应用相同的 index.html 页面。再次漂亮!

阿帕奇

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

【讨论】:

【解决方案2】:

这对我有用,因为我的 SPA 位于子文件夹中。将其写入 .htaccess 文件中。

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

网站文件夹:C:\xampp\htdocs\dist

.htaccess 文件名:C:\xampp\htdocs\dist\.htaccess

学分: https://gist.github.com/Prezens/f99fd28124b5557eb16816229391afee

【讨论】:

  • 欢迎来到 SO。请将链接的相关部分放在您的答案中,因为它们可能会过期或被删除。
  • 这为我节省了一天。谢谢
【解决方案3】:

嗨,我的朋友,如果您使用的是 Linux 操作系统,请走这条路线: /etc/apache2/sites-enabled/000-default.conf 并添加以下代码:

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

【讨论】:

  • 这给了我一个语法错误RewriteCond: unknown flag 'L' 。请帮忙。
【解决方案4】:

这似乎不是你的问题,我猜是apache .htaccess 问题, 您的本地可能有该文件,而您的服务器没有。

请检查一下,您也将 .htaccess 作为隐藏文件上传到您的服务器,您可能忘记上传了。

如果它不存在,您可以查看此参考帮助:https://router.vuejs.org/en/essentials/history-mode.html

如果不存在,请添加您自己的 .htaccess 文件。

【讨论】:

    猜你喜欢
    • 2020-06-17
    • 2019-03-27
    • 2020-12-06
    • 2021-01-22
    • 2013-02-14
    • 2020-08-22
    • 2020-01-10
    • 2020-05-18
    • 2011-03-04
    相关资源
    最近更新 更多