Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

devSSI's avatar
Level 5

Vue page cannot open after refreshing the page

My website is done using Vue, and since I am working and learning I have ran into the issue where my about page is actually separate page, and I have configured the router in js file and set /about to be component About.

On the first load, everything is fine but when I reload then the page crashes and it shows something like 404.

Anyone knows how to fix this? Thanks :)

0 likes
9 replies
LaryAI's avatar
Level 58

This issue typically occurs because when you refresh the page, the server tries to handle the route, but it doesn't know how to handle the Vue routes. To fix this, you need to configure your server to always serve the index.html file for any route that doesn't match a static file. This way, the Vue router can take over and handle the routing on the client side.

Here are solutions for some common server setups:

For an Apache Server

Create or update the .htaccess file in your project's root directory with the following content:

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

For an Nginx Server

Update your Nginx configuration file to include the following:

location / {
  try_files $uri $uri/ /index.html;
}

For a Node.js/Express Server

If you are using Express, you can add a catch-all route to serve the index.html file:

const express = require('express');
const path = require('path');
const app = express();

app.use(express.static(path.join(__dirname, 'dist')));

app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});

const port = process.env.PORT || 3000;
app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

For Vue CLI

If you are using Vue CLI, you can configure the vue.config.js file to handle the history mode of the Vue Router:

module.exports = {
  devServer: {
    historyApiFallback: true
  }
};

Vue Router Configuration

Ensure that your Vue Router is configured to use history mode:

import Vue from 'vue';
import Router from 'vue-router';
import About from './components/About.vue';

Vue.use(Router);

export default new Router({
  mode: 'history',
  routes: [
    {
      path: '/about',
      name: 'About',
      component: About
    },
    // other routes
  ]
});

By setting up your server to always serve the index.html file for any unknown routes, you allow the Vue Router to handle the routing on the client side, which should resolve the issue of the page not loading correctly after a refresh.

vincent15000's avatar

You say that when you reload the page ... how ? via a menu ? via the reload button of the browser ? another way ?

vincent15000's avatar

@devSSI Are you using Laravel for the backend ? If yes the problem is perhaps due to how you define the route.

Can you show your routes please ? (backend routes / frontend routes)

devSSI's avatar
Level 5

@vincent15000 No, it is separate app. It will be connected to some Laravel APIes but for now it is standalone

devSSI's avatar
Level 5

@MohamedTammam not sure which server is used, app is hosted on hostinger. Idk if they use apache or nginx

MohamedTammam's avatar

@devSSI Contact the support and tell them to point all request to index.html inside your Vue app.

devSSI's avatar
devSSI
OP
Best Answer
Level 5

For the future searches regarding this topic, the thing that fixed my issue is:

  1. If you don't have .htaccess file in you project root file on the production, create new one
  2. Add this into it
<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. Save and try it out!
1 like

Please or to participate in this conversation.