Subchapter 12.6
references/core-routing.mdMarkdown4 KBView on GitHub
VitePress uses file-based routing where markdown files map directly to HTML pages.
.
├─ index.md → /index.html (/)
├─ about.md → /about.html
├─ guide/
│ ├─ index.md → /guide/index.html (/guide/)
│ └─ getting-started.md → /guide/getting-started.html.
├─ docs # Project root
│ ├─ .vitepress # VitePress directory
│ │ ├─ config.ts # Configuration
│ │ ├─ theme/ # Custom theme
│ │ ├─ cache/ # Dev server cache (gitignore)
│ │ └─ dist/ # Build output (gitignore)
│ ├─ public/ # Static assets (copied as-is)
│ ├─ index.md # Home page
│ └─ guide/
│ └─ intro.mdSeparate source files from project root:
// .vitepress/config.ts
export default {
srcDir: './src' // Markdown files live in ./src/
}With srcDir: 'src':
.
├─ .vitepress/ # Config stays at project root
└─ src/ # Source directory
├─ index.md → /
└─ guide/intro.md → /guide/intro.htmlUse relative or absolute paths. Omit file extensions:
<!-- Recommended -->
[Getting Started](./getting-started)
[Guide](/guide/)
<!-- Works but not recommended -->
[Getting Started](./getting-started.md)
[Getting Started](./getting-started.html)Remove .html extension from URLs (requires server support):
export default {
cleanUrls: true
}Server requirements:
cleanUrls in vercel.jsontry_files $uri $uri.html $uri/ =404Customize the mapping between source and output paths:
export default {
rewrites: {
// Static mapping
'packages/pkg-a/src/index.md': 'pkg-a/index.md',
'packages/pkg-a/src/foo.md': 'pkg-a/foo.md',
// Dynamic parameters
'packages/:pkg/src/:slug*': ':pkg/:slug*'
}
}This maps packages/pkg-a/src/intro.md → /pkg-a/intro.html.
Important: Relative links in rewritten files should be based on the rewritten path, not the source path.
Rewrites can also be a function:
export default {
rewrites(id) {
return id.replace(/^packages\/([^/]+)\/src\//, '$1/')
}
}Files in public/ are copied to output root as-is:
docs/public/
├─ favicon.ico → /favicon.ico
├─ robots.txt → /robots.txt
└─ images/logo.png → /images/logo.pngReference with absolute paths:
For sub-path deployment (e.g., GitHub Pages):
export default {
base: '/repo-name/'
}All absolute paths are automatically prefixed with base. For dynamic paths in components, use withBase:
<script setup>
import { withBase } from 'vitepress'
</script>
<template>
<img :src="withBase('/logo.png')" />
</template>index.md files map to directory root (/guide/ instead of /guide/index)srcDir separates source from configcleanUrls removes .html but requires server supportrewrites enables complex source structures with clean output URLs