apache上的Vue-router,子目录中的SPA,只能通过重定向访问页面 [英] Vue-router on apache, SPA in sub-directory, can only access pages by redirect
问题描述
因此,我在apache开发服务器上为客户端设置了Vue应用.我这样做是为了配合生产环境.该应用程序位于子目录中,我在vue-router上将"base"选项设置为匹配.如果我导航到我的虚拟主机根目录,它会正确重定向,但是通过键入地址输入404导航到相同的地址.
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.
这是我的路由器代码:
const routes = [
{path: '/', redirect: '/london' },
{path: '/:city', component: homeView}
]
const router = new VueRouter ({
mode: 'history',
routes,
base: '/subdir/'
})
我在'subdir'中也有相关的Apaache .htaccess:
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>
因此,如果我导航到"cityapp.local/subdir",它将重定向到"cityapp.local/subdir/london",但是如果我输入"cityapp.local/subdir/london",则会显示apache 404页面.
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!
如果我将虚拟主机设置为包括子目录并从路由器中删除了基本选项,则一切正常.但是,我无法在生产中执行此操作,因此希望此信息对您有所帮助.
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.
推荐答案
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>
老实说,昨天我尝试过类似的事情.我将RewriteBase更改为子目录,但未更改重写规则!我很不喜欢.htaccesss的东西:(
I honestly tried something similar yesterday. I changed the RewriteBase to the subdir but not the rewrite rules! I'm bad at .htaccesss stuff :(
这篇关于apache上的Vue-router,子目录中的SPA,只能通过重定向访问页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!