最近專案Vite+Vue3佈署上IIS重新整理頁面會發生404,因此記錄下來解法:
第一種解法:
router/index.ts
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: []
})
改為
const router = createRouter({
history: createWebHashHistory(import.meta.env.BASE_URL),
routes: []
})
第二種解法:
在public下新增web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Handle History Mode and custom 404/500" 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="/" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
只能選一種解法,同時用的話會出錯