~/
Published on

Use Vercel as a Lightweight Redirector for Your Subdomains

Authors
  • avatar
    Name
    Hachiro
    Twitter

Short links should be boring, predictable, and easy to version. You do not need Nginx, a VPS, or a pet Node app. Vercel already supports permanent redirects with a simple config file, and it handles TLS, deploys, and rollbacks for you.

One tiny Vercel project can route git.farhad.my to GitHub today, resume.farhad.my to a resume page tomorrow, and more later, with each subdomain attached in Project Settings, DNS pointed to Vercel, and Middleware available if static redirects become limiting.

Vercel Specturm

Why would you even do this?

If you want vanity subdomains that jump to other services, Vercel gives you:

  • Permanent 301 or 308 redirects (good for SEO and caching)
  • Config in Git with vercel.json or framework native config
  • Fast deploys and easy rollback
  • TLS handled for you

That covers most personal and team link needs without servers or cron.


When this makes sense

Use this if you:

  • Prefer Git based config over ad hoc DNS forwarding
  • Want a single place to manage many subdomains
  • Might later graduate to host based logic in Middleware
  • Want wildcard capture for many short links under one project

Wildcard domains are supported, and the simplest path uses Vercel nameservers. There are alternatives if you cannot switch nameservers, with some caveats.


How to make it work

Two paths. Pick the one that fits.

Option A, zero code, vercel.json only (single destination)

  1. Create a tiny repo with a single vercel.json:
{
  "redirects": [
    {
      "source": "/(.*)",
      "destination": "https://github.com/hachirosan",
      "permanent": true
    }
  ]
}

Push to Vercel. This returns a permanent redirect for any path under the domain.

  1. Attach the subdomain to the project
    Vercel Dashboard, Project, Settings, Domains, Add Domain, enter git.farhad.my.

  2. Point DNS at Vercel
    If your DNS is outside Vercel, create a CNAME for git to Vercel’s CNAME target as shown in the UI. If you use Vercel DNS, add the record in the Domains page.

Visit https://git.farhad.my. You should land on GitHub with a proper permanent redirect.


Option B, many hostnames in one project, Next.js Middleware

If you want multiple subdomains to different destinations, handle it by Host header.

  1. Add Next.js and middleware.ts
// middleware.ts
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";

const hostMap: Record<string, string> = {
  "git.farhad.my": "https://github.com/hachirosan",
  "cv.farhad.my": "https://www.farhad.my/files/cv.pdf",
  "notes.farhad.my": "https://farhad-notes.notion.site"
};

export function middleware(req: NextRequest) {
  const host = (req.headers.get("host") || "").replace(/:\d+$/, "");
  const target = hostMap[host];
  if (target) {
    // 308 is permanent and preserves method
    return NextResponse.redirect(target, { status: 308 });
  }
  return NextResponse.next();
}

export const config = {
  matcher: ["/:path*"]
};

Middleware runs before routes, so this is fast and global, and you can grow the map as you add subdomains.

  1. Attach each subdomain in Project Settings, Domains
    Add git.farhad.my, cv.farhad.my, and so on. Keep DNS in sync as above.

Note, static redirects have a platform limit. If you expect a very large number of rules, use Middleware or a small function.


Optional, wildcard catch all

If you want many short links without pre adding each name, add a wildcard domain like *.links.farhad.my to the project. Wildcards are easiest with Vercel nameservers, which also enables automatic TLS. There are supported ways to delegate while keeping your registrar, check the docs for the current options.


Putting it all together

For a single redirect like git.farhad.my to GitHub, the vercel.json approach is the fastest. For many subdomains, keep one project and redirect by host in Middleware. Either way, all changes live in Git, you get clean 308 or 301 responses, and you do not run servers just to forward links.