0 votes
61 views
in General by
Each time refreshing the Vue.js page I am getting a 404 error, it has been my headache for a while, anyone can advise?

1 Answer

0 votes
by

When you refresh a Vue.js page, you may receive a 404 error if the server is not configured to handle the URL correctly. By default, Vue.js uses the HTML5 History mode, which does not include the hash symbol (#) in the URL. This can cause problems with server configuration, especially if you are using a web server that does not support URL rewriting.

To fix this issue, you need to configure your webserver to handle the URL correctly. If you are using Apache, you can use the mod_rewrite module to rewrite the URL. You can create a .htaccess file in your root directory and add the following code:

<IfModule mod_rewrite.c>

  RewriteEngine On

  RewriteBase /

  RewriteRule ^index\.html$ - [L]

  RewriteCond %{REQUEST_FILENAME} !-f

  RewriteCond %{REQUEST_FILENAME} !-d

  RewriteRule . /index.html [L]

</IfModule>

This code will redirect all requests to the index.html file, which is the entry point of your Vue.js application. It will also preserve the original URL, so the user will not see any changes in the address bar.

If you are using a different web server, you may need to consult its documentation to find out how to configure it to handle Vue.js URLs correctly.

In addition to configuring your web server, you can also use the history mode with a base configuration option. You can add the following code to your router.js file:
 

const router = new VueRouter({

  mode: 'history',

  base: '/your/base/path/',

  routes: [

    // ...

  ]

})

This code will set the base path for your Vue.js application, so the router will handle all URLs relative to that path. This can help avoid conflicts with other URLs on your website.

I hope this helps! Let me know if you have any other questions.

...