vue 项目部署到iis后 浏览器刷新404

最新写了一个vue的demo,部署到iis后,正常router跳转都没问题,但是点击浏览器的刷新按钮后,就会出现404找不到文件的现象。

解决办法

1.首先需要确定iis是否下载了rewrite 模块,如果没有下载,需要下载一个安装上:
rewrite

2.vue项目下添加一个web.config文件:

csharp 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <!-- 启用 URL Rewrite 模块 -->
    <rewrite>
      <rules>
        <!-- 排除实际文件和目录 -->
        <rule name="Static Files" stopProcessing="true">
          <match url="^(.*)$" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" />
          </conditions>
          <action type="None" />
        </rule>
        
        <rule name="Directories" stopProcessing="true">
          <match url="^(.*)$" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" />
          </conditions>
          <action type="None" />
        </rule>
        
        <!-- Vue Router History 模式:所有其他请求重定向到 index.html -->
        <rule name="Vue Router History Mode" stopProcessing="true">
          <match url="^(.*)$" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="/index.html" />
        </rule>
      </rules>
    </rewrite>
    

    
    <!-- 压缩配置(可选但推荐) -->
    <httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
      <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" staticCompressionLevel="9" />
      <dynamicTypes>
        <add mimeType="text/html" enabled="true" />
        <add mimeType="text/plain" enabled="true" />
        <add mimeType="text/css" enabled="true" />
        <add mimeType="text/javascript" enabled="true" />
        <add mimeType="application/javascript" enabled="true" />
        <add mimeType="application/json" enabled="true" />
      </dynamicTypes>
    </httpCompression>
    
    <!-- 安全头配置 -->
    <httpProtocol>
      <customHeaders>
        <add name="X-Content-Type-Options" value="nosniff" />
        <add name="X-Frame-Options" value="SAMEORIGIN" />
        <add name="X-XSS-Protection" value="1; mode=block" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

根据自己选择添加即可。配置完毕后,放到部署的dist文件夹里即可,再访问网址就可以随便刷新了。

相关推荐
CharlesYu016 小时前
前端性能优化的第一性原理,是不断缩短“用户发起意图 → 获得可用结果”之间的时间
前端
Bs_MoneyMagnet6 小时前
基于springboot+vue的医疗健康便民服务平台的设计与实现 源码+文档
java·vue.js·spring boot·后端·spring
平头哥技术团队6 小时前
Day 21 _ 页内锚点_给每段起个 id,目录写 href=_#id_,点一下页面就滚到那一段
前端·html·html5
子兮曰7 小时前
Bun v1.4.1 深度解析:从 Zig 到 Rust,一场 11 天、64 个 AI 代理的语言迁徙
前端·后端·bun
人民广场吃泡面8 小时前
什么是AI Agent?它又能给前端带来哪些效率提升?
前端·人工智能
中科三方8 小时前
两家域名注册商资质被ICANN终止:企业域名资产安全再受关注
前端·网络·安全·域名
bug总结8 小时前
uniapp vue3全局方法注册使用
前端·javascript·uni-app
华无丽言9 小时前
如何在宜搭中实现获取子表中的字段值赋值到父表中?
前端·javascript·低代码
IT_陈寒9 小时前
Vue的computed属性竟然坑了我一把
前端·人工智能·后端
威斯软科的老司机9 小时前
通俗讲解 CNN 图像识别、向量 Embedding、Softmax 概率计算这三块的简化原理
前端·人工智能·ui·数字孪生