Vare路由器托管在apache2上

时间:2017-12-19 04:13:34

标签: apache vue.js vuejs2 vue-router

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

  1. init project
  2. 设置路由器
  3. 构建并托管到apache服务器
  4. 
    
    const router = new VueRouter({
      routes: Routes,
      // relative: true,
      mode: 'history'
    });
    
    
    

    这在本地运行正常,但vue路由器在服务器上中断。 实施例

    如果我运行http://example.com并转到http://exmaple.com/sub 它工作正常

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

    错误: enter image description here

4 个答案:

答案 0 :(得分:11)

来自vue.js documentation page

  

使用历史记录模式时,网址会看起来正常,"例如http://oursite.com/user/id。美丽!

     

但是出现了一个问题:由于我们的应用是单页客户端应用,如果没有正确的服务器配置,如果用户直接在浏览器中访问http://oursite.com/user/id,则会收到404错误。现在那很难看。

     

不用担心:要解决此问题,您需要做的就是向服务器添加一个简单的全部回退路由。如果网址与任何静态资源不匹配,则该网址应该与您的应用所在的index.html页面相同。再次美丽!

的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>

答案 1 :(得分:1)

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

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

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

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

答案 2 :(得分:1)

您好,我的朋友,如果您使用的是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>

答案 3 :(得分:0)

这对我有用,因为我的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