Vue Router return 404 when revisit to the url

That is too simple, you just have to add this config on your server:

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>

Then if you are on Nginx:

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

And that's all

For more information, visit this Link


I think you are missing that SPA is not server side rendering. At least for the majority. So, when you access /anything your web server won't redirect it to index.html. However, if you click on any vuejs router link, it will work due to the fact that the javascript got in action, but not the server side.

To solve this, use .htaccess and redirect all requests to index.html like so

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

Hope it helps someone!


By refreshing the page you are making a request to the server with the current url and the server returns 404. You have to handle this on your web framework or web server level.

This article contains example server confiruation: https://router.vuejs.org/guide/essentials/history-mode.html