21

I am hosting my vuejs project on apache server.

  1. init project
  2. setup router
  3. Build and hosted to apache server

const router = new VueRouter({
  routes: Routes,
  // relative: true,
  mode: 'history'
});

This runs fine in local, but vue router breaks on server. Example

If I run http://example.com and go to http://exmaple.com/sub It works fine

but if I refresh the page It will throw 404 Error.

Error: enter image description here

1

5 Answers 5

43

From the vue.js documentation page:

When using history mode, the URL will look "normal," e.g. http://oursite.com/user/id. Beautiful!

Here comes a problem, though: Since our app is a single page client side app, without a proper server configuration, the users will get a 404 error if they access http://oursite.com/user/id directly in their browser. Now that's ugly.

Not to worry: To fix the issue, all you need to do is add a simple catch-all fallback route to your server. If the URL doesn't match any static assets, it should serve the same index.html page that your app lives in. Beautiful, again!

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>
Sign up to request clarification or add additional context in comments.

3 Comments

is there a way to automatically include .htaccess into npm run build result? I created a new question about it stackoverflow.com/questions/52397542/…
Where do i write this?
@Desmond httpd.conf
10

This worked for me, because my SPA is in a subfolder.Write this inside the .htaccess file.

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

Website folder: C:\xampp\htdocs\dist

.htaccess file ubication: C:\xampp\htdocs\dist\.htaccess

Credits: https://gist.github.com/Prezens/f99fd28124b5557eb16816229391afee

1 Comment

Welcome to SO. Please put the relevant part of the link in your answer as they may get expired or be removed.
10

Since Apache 2.2.16, mod_rewrite is no longer necessary for this. Simply use FallbackResource under the <Directory> or <VirtualHost> configuration.

FallbackResource /index.html

This will serve index.html for all URLs that do not map to anything.

Comments

2

Hi my friend if you are using Linux os, go this routes : /etc/apache2/sites-enabled/000-default.conf And add the following code :

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

1 Comment

This gives me a syntax error RewriteCond: unknown flag 'L' . Help plz.
1

It seems its not your issue its apache .htaccess issue i guess, your local may have that file and your server don't have it.

please once check it that, you also uploaded .htaccess to your server as its hidden file you may forgot to upload it.

if its not there you can check this reference help : https://router.vuejs.org/en/essentials/history-mode.html

and add your own .htaccess file if its not there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.