栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

uniapp h5部署到服务器刷新页面出现404

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

uniapp h5部署到服务器刷新页面出现404

解决方案一:

将uniapp的manifest.json中h5配置的路由模式设为hash模式

优点:当前端没办法接触到服务器的时候,简单修改下配置就能修复

缺点:换成hash后url中带#号,不美观,而且会影响传参

不能接受这个缺点可以参考解决方案二。

解决方案二:

路由模式设置为history的同时简单配置下服务器即可

①.Apache


  RewriteEngine On
  Rewritebase /
  RewriteRule ^index.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.html [L]

 

除了 mod_rewrite,你也可以使用 FallbackResource (opens new window)。

②.nginx

location / {
  try_files $uri $uri/ /index.html;
}

③.原生 Node.js

const http = require('http')
const fs = require('fs')
const httpPort = 80

http.createServer((req, res) => {
  fs.readFile('index.html', 'utf-8', (err, content) => {
    if (err) {
      console.log('We cannot open "index.html" 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)
})

④.基于 Node.js 的 Express

对于 Node.js/Express,请考虑使用 connect-history-api-fallback 中间件。

⑤.Internet Information Services (IIS)

  1. 安装 IIS UrlRewrite(opens new window)
  2. 在你的网站根目录中创建一个 web.config 文件,内容如下:
    
    
      
        
          
            
              
              
                
                
              
              
            
          
        
      
    

⑥.Candy

rewrite {
    regexp .*
    to {path} /
}

⑦.Firebase 主机

{
  "hosting": {
    "public": "dist",
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}

友情提示:这么做以后,你的服务器就不再返回 404 错误页面,因为对于所有路径都会返回 index.html 文件。为了避免这种情况,你应该在 Vue 应用里面覆盖所有的路由情况,然后再给出一个 404 页面。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/643460.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号