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 OnRewriteBase /RewriteRule ^index\.html$ - [L]RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . /index.html [L] </IfModule>
第三種?原生 Node.js
const http = require('http') const fs = require('fs') const httpPort = 80http.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