history模式配置后刷新404的解决办法!
第一种 nginx配置
在usr/local/nginx/conf/vhost 下 域名.conf配置文件修改或添加
第一种方案
server
{ ##在server下添加或在location里面添加以下代码 location / { if (!-e $request_filename) { rewrite ^(.*)$ /index.html?s=$1 last; break; } } ## 如果访问的不是根目录用下面方式设置 qiancheng是我的子目录 location /qiancheng{ if (!-e $request_filename) { rewrite ^/(.*) /qiancheng/index.html last; break; } } }
第二种方案
location / {
try_files $uri $uri/ /index.html;
}
第二种 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>
第三种 原生 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) })
第四种 Firebase 主机
在你的 firebase.json 中加入: { "hosting": { "public": "dist", "rewrites": [ { "source": "**", "destination": "/index.html" } ] } }
第五种 tomcat的配置
添加 WEB-INF文件夹web.xml文件 加入下列代码:
<web-app>
<error-page>
<error-code>404</error-code>
<location>/index.html</location>
</error-page>
</web-app>
参考文件
https://router.vuejs.org/zh/guide/essentials/history-mode.html#%E5%90%8E%E7%AB%AF%E9%85%8D%E7%BD%AE%E4%BE%8B%E5%AD%90