Redirect Management System

This project uses Sanity CMS to manage URL redirects for the First15 website. Redirects are automatically deployed via Netlify on every build.

Overview

Redirects are managed in Sanity Studio and automatically fetched during the Eleventy build process to generate Netlify's _redirects file.

How It Works

  1. Sanity CMS - Content editors manage redirects through the Sanity Studio UI
  2. Eleventy Build - Fetches published redirects from Sanity and filters by go-live date
  3. Netlify - Uses the generated _redirects file to handle URL redirections

Deployment Triggers

Redirects are deployed automatically when:

Managing Redirects in Sanity

Accessing Redirects

  1. Navigate to Website > Redirects in Sanity Studio
  2. Choose from:

Creating a New Redirect

  1. Go to Website > Redirects > All Redirects

  2. Click Create

  3. Fill in the required fields:

  4. Click Publish

Field Details

Source Path

Destination Path

Redirect Type

Go Live Date

Importing Existing Redirects

  1. Go to Website > Redirects > Import Redirects
  2. Paste the contents of your existing _redirects file
  3. Click Parse Redirects to preview
  4. Review the parsed redirects
  5. Click Import to Sanity to create all redirect documents

Viewing Active Redirects

Go to Website > Redirects > _redirects File Preview to see:

Viewing Redirect History

Go to Website > Redirects > Redirect History & Audit Log to see:

Technical Implementation

File Structure

first15web/
├── _data/
│   └── redirects.js          # Fetches redirects from Sanity with date filtering
├── redirects.liquid           # Template that generates _redirects file
└── _site/
    └── _redirects            # Generated file (deployed to Netlify)

Data Fetching (_data/redirects.js)

// Fetches published redirects from Sanity
const query = `*[_type == "redirect" && !(_id in path("drafts.**"))] | order(source_path asc)`;

// Filters by go_live_date
const activeRedirects = data.filter((redirect) => {
  if (!redirect.go_live_date) return true; // Active immediately
  return new Date(redirect.go_live_date) <= today;
});

Caching

Template (redirects.liquid)

Generates the _redirects file in Netlify format:

[source] [destination] [status_code]

Example output:

/old-page /new-page 301
/store https://store.example.com 302
/blog/* /articles/:splat 301

Netlify Redirect Format

The generated _redirects file follows Netlify's redirect syntax:

Basic Redirect

/old-path /new-path 301

Splat (Wildcard)

/old-section/* /new-section/:splat 301

Placeholders

/blog/:slug /articles/:slug 301

Domain Redirects

https://olddomain.com/* https://newdomain.com/:splat 301

Query Parameters

/search /results?q=:query 302

Development

Local Development

The system pulls from the production Sanity dataset by default. Changes published in Sanity will appear in local builds after the cache expires (5 minutes).

To rebuild and see updated redirects:

cd first15web
npx @11ty/eleventy
cat _site/_redirects  # View generated file

Testing Redirects Locally

  1. Create/edit a redirect in Sanity Studio
  2. Publish the redirect
  3. Wait 5 minutes for cache to expire (or delete .cache folder)
  4. Rebuild: npx @11ty/eleventy
  5. Check _site/_redirects for your changes

Removing Old Static File

The old static _redirects file is no longer used. The Eleventy config previously had:

eleventyConfig.addPassthroughCopy('./_redirects'); // ❌ REMOVED

This line was removed because redirects are now dynamically generated from Sanity.

Troubleshooting

Redirect Not Working

  1. Check if published: Drafts are not included in builds
  2. Check go-live date: Future-dated redirects won't be active yet
  3. Check the preview: Use "_redirects File Preview" in Sanity to confirm it's included
  4. Clear cache: Delete .cache folder in first15web and rebuild
  5. Verify deployment: Check that Netlify deployed the updated _redirects file

Duplicate Redirects

If you see a conflict warning in Sanity:

Redirect Not Appearing

Redirects won't appear in builds if:

Build Errors

If Eleventy build fails:

  1. Check that @sanity/client is installed: npm list @sanity/client
  2. Verify Sanity credentials in utils/sanityClient.js
  3. Check console for GROQ query errors
  4. Test the query in Sanity Vision: https://first15.sanity.studio/vision

Best Practices

Choosing Redirect Type

Go-Live Dates

Source Paths

Notes Field

Use the notes field to document:

Additional Resources