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

相关推荐
我材不敲代码1 小时前
Python 函数核心:位置参数与关键字参数详解
java·前端·python
丷丩1 小时前
MapLibre GL JS第8课:禁用滚动缩放
javascript·mapbox·maplibre gl js
Kratzdisteln1 小时前
【无标题】
前端·python
李剑一2 小时前
小红书前端架构面试问的挺深入啊!面试官:Vue中组合式API与选项式API的设计权衡
vue.js·面试
Curvatureflight2 小时前
前端国际化 i18n 落地实践:语言包、动态文案和格式化问题怎么处理?
前端·c++·vue
kTR2hD1qb2 小时前
Claude Code Skill的介绍与使用
java·前端·数据库·人工智能
一 乐2 小时前
汽车租赁|基于SprinBoot+vue的汽车租赁管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·汽车·论文·毕设·汽车租赁管理系统
修己xj3 小时前
打造专属博文封面神器:一个开源免费的博文封面生成器ThisCover
前端
kyriewen3 小时前
面试8家前端岗位后,我发现了一个残酷的事实:AI不是加分项,是门槛
前端·javascript·面试
Fighting_p3 小时前
【面试 - el-select问题及解决】wujie 微前端下子系统 el-select 多选 filterable 过滤失效
前端