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文件夹里即可,再访问网址就可以随便刷新了。

相关推荐
绝世唐门三哥21 小时前
OpenClaw 安装 + 手动配置 DeepSeek 模型(2026.4.5 版)
前端·oepn claw
来一颗砂糖橘21 小时前
吃透 ES6 扩展运算符(...):从表面语法到底层逻辑,避开所有坑
前端·javascript·es6·扩展运算符·前端进阶
前端小D1 天前
JS模块化
开发语言·前端·javascript
Muen1 天前
iOS开发-适配XCode26、iOS26
前端
ByteCraze1 天前
JavaScript 深拷贝完全指南:从入门到精通
开发语言·javascript·ecmascript
用户84298142418101 天前
3个Html加密工具
javascript
卸任1 天前
Electron霸屏功能总结
前端·react.js·electron
fengci.1 天前
ctfshow黑盒测试前半部分
前端
忆琳1 天前
Vue3 优雅解决单引号注入问题:自定义指令 + 全局插件双方案
vue.js·element
忆琳1 天前
Vue3 全局自动大写转换:一个配置,全站生效
javascript·element