路由设置为 History 模式 404 问题
如果路由模式使用 History,需要后端的配合,否则容易出现 404 错误
1. 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>
2 nginx 配置
在配置文件的 server
下,添加如下代码:
location / {
try_files $uri $uri/ /index.html;
}
3 Node.js 配置
const http = require('http')
const fs = require('fs')
const httpPort = 80
http.createServer((req, res) => {
fs.readFile('index.htm', 'utf-8', (err, content) => {
if (err) {
console.log('We cannot open "index.htm" file.')
}
res.writeHead(200, {
'Content-Type': 'text/html; charset=utf-8'
})
res.end(content)
})
}).listen(httpPort, () => {
console.log('Server listening on: http://localhost:%s', httpPort)
})
注意: Vue 文档中有相关介绍: https://router.vuejs.org/zh/guide/essentials/history-mode.html