You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vue Router路径正则排除assets目录,解决Chromium下重定向异常

问题:Chromium内核浏览器下,Vue路由仍会重定向已在服务端排除的assets目录请求

我需要排除public assets目录的路由重定向,已在服务端完成排除配置,但从Web应用内点击对应URL时仍会触发重定向——该问题仅在Chromium内核浏览器中出现,硬刷新或使用Firefox时无异常。

尝试过的配置

初始404路由配置

原本使用通配符路由将所有请求导向404页面:

{path: '/:pathMatch(.*)', component: NotFound}

正则表达式尝试

使用负向预查正则表达式匹配非assets路径:(?!^\/?assets\/?.*$)(^.*$)

调整后的路由配置

尝试将正则直接写入路由path:

const routes = [
    {path: '/index.html', redirect: '/'},
    {path: '/', component: Home},
    {path: '/portfolio', component: Portfolio},
    {path: '/cv', component: Resume},
    {path: '/contact', component: Contact},
    {path: '(?!^\\/?assets\\/?.*$)(^.*$)', component: NotFound}
]

也试过将正则嵌套在动态参数中:

{path: '/:pathMatch((?!^\\/?assets\\/?.*$)(^.*$))', component: NotFound}

以上配置均未生效。

已查阅资料

  • Vue Router路径正则使用方法
  • Vue Router官方文档:通配符匹配与404路由
  • path-to-regexp库文档
  • 正则表达式入门指南

项目环境与服务器配置

项目基于Node.js + Vite + Vue.js + TypeScript搭建,服务器使用Netlify,配置文件netlify.toml内容如下:

# 重定向规则
[[redirects]]
from = "/assets/cv.pdf"
to = "/cv"
status = 301

[[redirects]]
from = "/cv.html"
to = "/cv"
status = 301

[[redirects]]
from = "/contact.html"
to = "/contact"
status = 301

[[redirects]]
from = "/index.php"
to = "/"
status = 301

# 默认favicon重定向
[[redirects]]
from = "/favicon.ico"
to = "/assets/img/logo.png"
status = 301

# 页面路由
[[redirects]]
from = "/"
to = "/index.html"
status = 200

[[redirects]]
from = "/portfolio"
to = "/index.html"
status = 200

[[redirects]]
from = "/cv"
to = "/index.html"
status = 200

[[redirects]]
from = "/contact"
to = "/index.html"
status = 200

# 公开资源目录
[[redirects]]
from = "/assets/*"
to = "/assets/:splat"
status = 200
force = true

# 404规则
[[redirects]]
from = "/*"
to = "/index.html"
status = 404

# Netlify构建触发条件:仅当src目录有变更时才构建
[build]
ignore = "git diff --quiet $COMMIT_REF $CACHED_COMMIT_REF -- src/"

内容的提问来源于stack exchange,提问作者Remzi Cavdar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 19:20:06