Routing

File-based routing, source directory structure, clean URLs, and route rewrites

VitePress uses file-based routing where markdown files map directly to HTML pages.

File to URL Mapping

.
├─ index.md           →  /index.html (/)
├─ about.md           →  /about.html
├─ guide/
│  ├─ index.md        →  /guide/index.html (/guide/)
│  └─ getting-started.md → /guide/getting-started.html

Project Structure

.
├─ 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.md

Source Directory

Separate source files from project root:

With srcDir: 'src':

Linking Between Pages

Use relative or absolute paths. Omit file extensions:

Clean URLs

Remove .html extension from URLs (requires server support):

Server requirements:

  • Netlify, GitHub Pages: Supported by default

  • Vercel: Enable cleanUrls in vercel.json

  • Nginx: Configure try_files $uri $uri.html $uri/ =404

Route Rewrites

Customize the mapping between source and output paths:

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:

Public Directory

Files in public/ are copied to output root as-is:

Reference with absolute paths:

Base URL

For sub-path deployment (e.g., GitHub Pages):

All absolute paths are automatically prefixed with base. For dynamic paths in components, use withBase:

Key Points

  • index.md files map to directory root (/guide/ instead of /guide/index)

  • Use paths without extensions in links for flexibility

  • srcDir separates source from config

  • cleanUrls removes .html but requires server support

  • rewrites enables complex source structures with clean output URLs

Last updated

Was this helpful?