【问题标题】:404 error on page reload in vue router history modeVue路由器历史模式下页面重新加载时出现404错误
【发布时间】:2023-03-20 01:31:01
【问题描述】:

我在 ubuntu(apache2 服务器)上部署了 vue cli 项目 vue路由模式改变后,除了index之外的所有页面,在reload时都显示404页面。

我将 .htaccess 添加到 index.html 所在的 /dist 路径,但它仍然无法正常工作 我不知道还能做什么来解决这个问题。 有人知道吗?

这是我的项目 dist 文件夹

/home/admin/web/mali-nali.com/public_html/dist

dist 中的.htaccess 代码

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

这是 htacccess.madewithlove.be 调试信息

RewriteEngine On    RewriteEngine was now turned on
2   RewriteBase /   Using / as the base for the rewrites.
3   RewriteRule ^index\.html$ - [L] This rule was not met.
4   RewriteCond %{REQUEST_FILENAME} !-f This condition was met.
5   RewriteCond %{REQUEST_FILENAME} !-d This condition was met.
6   RewriteRule . /index.html [L]   The new url is https://mali-nali.com/index.html
The tests are stopped because of the L in your RewriteRule options.

所有不存在页面的最后一条路径

{path: '/*',component:FourohFour, name:'404'}

【问题讨论】:

    标签: apache .htaccess vue.js vue-router


    【解决方案1】:

    来自https://router.vuejs.org/guide/essentials/history-mode.html#example-server-configurations

    注意:以下示例假设您从根文件夹提供应用程序。如果您部署到子文件夹,则应使用 Vue CLI 的 publicPath 选项和路由器的相关基本属性。

    看来这就是你所缺少的。

    【讨论】:

    • 我不明白,如果apache服务器指向dist文件夹,那是否意味着根文件夹是dist?我也试过这个配置 .htacccess 的子文件夹,它仍然不工作``` RewriteEngine On RewriteBase /dist/ RewriteRule ^index\.html$ - [L] RewriteCond %{REQUEST_FILENAME} ! - f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule 。 /index.html [L] ```
    【解决方案2】:

    在 Vue 项目的根目录中添加一个名为 vue.config.js 的文件,代码如下。

    module.exports = {
     publicPath: process.env.NODE_ENV === 'production' ? '/mali-nali.com/' : '/'
    }
    

    然后仍然在你的 Vue 项目的根目录中添加一个名为 404.html 的文件,代码如下。

    <!DOCTYPE html>
     <html>
      <head>
        <script>         
            sessionStorage.redirect = location.href;
        </script>
        <meta http-equiv="refresh" content="0;URL='/mali-nali.com/'">
      </head>
     </html>
    

    然后将您创建的 404.html 文件复制到 /dist 文件夹中。 希望对你有帮助。

    【讨论】:

    • 这行不通。我的 public_path 是 '/',在所有这些更改之后,索引页面没有打开。
    • 在路由文件中尝试更改为:{ path: '*', redirect: '/404', }
    【解决方案3】:

    在 vue.config.js 中设置您的 publicPath 选项:

    module.exports = {
      publicPath: process.env.NODE_ENV === "production" ? "/subfolder-name/" : "/",
    };
    

    然后,在 dist 目录中,创建一个 .htaccess,其内容如下:

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

    【讨论】:

      【解决方案4】:

      因为这个问题在今天和当时一样重要,这可能是一个错误,为什么您在刷新 SPA 路由时会收到 404。 如果您将 laravel 和 Vue SPA 与 Vue Router 一起使用,则必须确保 Web 路由配置正确。

      这一行应该在你的 web.php 中像这样或类似的:

      web.php

      Route::get('{any}', function () {
          return view('your-spa-page'); 
         // your-spa-page = the page in which you include the SPA via the selector #app
      })->where('any', '.*');
      

      【讨论】:

        猜你喜欢
        • 2021-06-16
        • 2021-09-27
        • 2023-01-26
        • 2021-06-05
        • 1970-01-01
        • 2020-02-14
        • 1970-01-01
        • 2020-06-20
        • 1970-01-01
        相关资源
        最近更新 更多