10

So I have a Vue app for a client set up on an apache dev server. I am doing this to match the production environment. The app is in a subdirectory and I set the 'base' option on vue-router to match. If I navigate to my virtual host root it redirects properly, however navigating to the same address by typing the address in gives a 404.

Here is my router code:

const routes = [
  {path: '/', redirect: '/london' },
  {path: '/:city', component: homeView}
]

const router = new VueRouter ({
  mode: 'history',
  routes,
  base: '/subdir/'
})

I also have the relevant Apaache .htaccess in the 'subdir':

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

So if I navigate to "cityapp.local/subdir" it redirects to "cityapp.local/subdir/london", but if I type "cityapp.local/subdir/london" I got the apache 404 page.

Any thoughts or help would be appreciated!

EDIT: If I set my virtual host to include the subdir and removed the base option from the router, everything works fine. However, I cannot do this on production so I hope this info helps a little.

3
  • do you have the .htaccess file placed in your subdir folder? Commented Aug 31, 2018 at 16:18
  • Yes. I figured it out. See my answer below. Commented Aug 31, 2018 at 16:28
  • 7 years later, a better solution: just use Caddy, lol Commented Apr 17 at 13:06

4 Answers 4

37

Jeez, was looking for a solution to this for a good chunk of time yesterday and today and just found the answer: Vue help forum: Vue webpack project path change

Relevant code for anyone else that my find this:

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

I honestly tried something similar yesterday. I changed the RewriteBase to the subdir but not the rewrite rules! I'm bad at .htaccesss stuff :(

Sign up to request clarification or add additional context in comments.

5 Comments

oh yes, the example in vue docs doesn't show use within subdirectories
I recently had the same problem and came across a great tool to help debug htaccess issues: htaccess.madewithlove.be
My project was deployed in a subdirectory on an existing server with 404 handling that had to be maintained. To make the above solution work in this case add one additional RewriteCond to the existing two.RewriteCond %{REQUEST_URI} ^/subdirectoryName/. [NC]
Thanks for the link Cliff. This tool finally demystified what is going on in my htaccess file.
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase /jcafe/dist RewriteRule ^jcafe/dist/index\.html$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /jcafe/dist/index.html [L] </IfModule> I got 403 please help.
8

It can be a bug with your apache version.

RewriteRule of "^$" broke between 2.2.21 and 2.4.2 https://bz.apache.org/bugzilla/show_bug.cgi?id=53929

You can use Fallback ressource instead of mod_rewrite in your apache config. It works for me.

In /etc/apache2/apache2.conf

<VirtualHost *:80>
    ServerName YourServerName
    DocumentRoot /var/www/yourApp/dist
    <Directory "/var/www/yourApp/dist">
        FallbackResource /index.html
    </Directory>
</VirtualHost>

Comments

4

i fixed this by adding FallbackResource /index.html in my site configuration...

the directory in /etc/apache2/sites-enabled/{your domain}.conf

DocumentRoot /var/www/yourApp/dist
<Directory "/var/www/yourApp/dist">
    FallbackResource /index.html
</Directory>

Comments

0

I didn't think of this one: if your URL's contain encoded forward slashes like %2F, Apache will always return 404 in rewrites. This can be controlled via the AllowEncodedSlashes directive, like so:

AllowEncodedSlashes NoDecode

This will make the Vue router work as intended.
https://httpd.apache.org/docs/2.4/mod/core.html#allowencodedslashes

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.