Redirect Management Plugin

Manage URL redirects with exact, wildcard, and regex matching. Includes CSV bulk import/export, analytics tracking, and optional Cloudflare Bulk Redirects integration.

Overview

The Redirect Management plugin handles URL redirects at the application level with sub-millisecond lookups via an LRU cache. It supports multiple matching strategies and integrates with Cloudflare's edge network for maximum performance.

πŸ”€

Flexible Matching

Exact, wildcard, and regex URL matching with priority ordering

πŸ“Š

Analytics

Track hit counts and last-hit timestamps for every redirect

πŸ“‹

Bulk Operations

CSV import/export with validation and duplicate handling

☁️

Cloudflare Edge

Sync eligible redirects to Cloudflare Bulk Redirects for edge-level processing


Setup

The Redirect Management plugin is a core plugin. Activate it in the admin panel:

  1. Navigate to Admin β†’ Plugins
  2. Find Redirect Management
  3. Click Activate

The plugin creates two database tables: redirects and redirect_analytics.

Access the admin UI at Admin β†’ Redirects in the sidebar.


Creating Redirects

Match Types

TypeDescriptionCloudflare Eligible
ExactURL must match exactlyYes
WildcardPrefix/contains matchingYes
RegexRegular expression patternNo

Status Codes

CodeMeaningUse Case
301Moved PermanentlySEO-safe permanent redirects
302Found (Temporary)Temporary redirects
307Temporary RedirectPreserves HTTP method
308Permanent RedirectPreserves HTTP method
410GoneContent permanently removed (no redirect)

Redirect Options

Each redirect supports Cloudflare-aligned options:

OptionDescription
Preserve Query StringForward query parameters to destination
Include SubdomainsMatch across subdomains
Subpath MatchingMatch subpaths under the source URL
Preserve Path SuffixAppend matched subpath to destination

Admin Form

Create redirects via the admin UI at /admin/redirects/new:

  1. Enter Source URL (e.g., /old-page)
  2. Enter Destination URL (e.g., /new-page or https://example.com)
  3. Select Status Code and Match Type
  4. Toggle options as needed
  5. Click Create Redirect

The plugin validates for circular redirects and duplicate sources before saving.


CSV Import & Export

Exporting

Click Export CSV on the redirects list page. The export respects any active filters. CSV columns:

id, source_url, destination_url, match_type, status_code, active,
preserve_query_string, include_subdomains, subpath_matching,
preserve_path_suffix, created_at, updated_at

Importing

Click Import CSV and upload a CSV file (max 10MB). Required columns: source_url and destination_url.

Duplicate Handling Strategies:

StrategyBehavior
RejectFail entire import if any duplicate is found
SkipImport only new redirects, skip duplicates
UpdateOverwrite existing redirects with CSV values

Validation checks:

  • URL format validation (must start with /, http://, or https://)
  • Status code validation (301, 302, 307, 308, 410)
  • Intra-file duplicate detection
  • Database duplicate detection
  • Circular redirect chain detection

If errors are found, the system returns an error CSV with line numbers and error messages for each invalid row.

CSV Import Example

source_url,destination_url,match_type,status_code,active
/old-blog,/blog,exact,301,true
/legacy/*,/new/*,wildcard,301,true
/temp-page,/landing,exact,302,true

Cloudflare Integration

Sync eligible redirects to Cloudflare Bulk Redirects for edge-level processing, eliminating the need to hit your Workers for common redirects.

Requirements

Set these environment variables:

CLOUDFLARE_API_TOKEN=your_token_here
CLOUDFLARE_ACCOUNT_ID=your_account_id

The API token needs Account Filter Lists Edit and Account Rulesets Edit permissions.

Eligibility

A redirect is synced to Cloudflare only if:

  • Match type is Exact or Wildcard (not Regex)
  • Status code is 301, 302, 307, or 308 (not 410)
  • Redirect is active

Sync Modes

Auto-sync on create: Enable autoOffloadEnabled in plugin settings. Eligible redirects are automatically synced to Cloudflare after creation.

Manual sync: Click Sync to Cloudflare in the admin UI to sync all eligible redirects. This clears existing Cloudflare items and replaces them with the current set.

Real-time sync: Individual redirects are synced to Cloudflare after create/update operations (non-blocking).

Cloudflare Resources

The plugin creates and manages:

  • A redirect list named sonicjs-redirects
  • A ruleset named SonicJS Bulk Redirects in the http_request_redirect phase

API Reference

REST API

All API routes are mounted at /api/redirects and support Bearer token authentication via the REDIRECTS_API_KEY environment variable.

GET/api/redirectsAuth required
List redirects with filtering and pagination

Query parameters: isActive, statusCode, matchType, search, limit (default 50), offset

GET/api/redirects/:idAuth required
Get a single redirect
POST/api/redirectsAuth required
Create a redirect

Create Redirect

curl -X POST http://localhost:8787/api/redirects \
  -H "Authorization: Bearer {api_key}" \
  -H "Content-Type: application/json" \
  -d '{
    "source": "/old-page",
    "destination": "/new-page",
    "matchType": 0,
    "statusCode": 301,
    "preserveQueryString": false
  }'
PUT/api/redirects/:idAuth required
Update a redirect
DELETE/api/redirects/:idAuth required
Soft-delete a redirect

Admin Routes

MethodPathDescription
GET/admin/redirectsList page with filters and pagination
GET/admin/redirects/newCreate form
GET/admin/redirects/:id/editEdit form with audit trail
POST/admin/redirectsCreate redirect
PUT/admin/redirects/:idUpdate redirect
DELETE/admin/redirects/:idDelete redirect
POST/admin/redirects/bulk-deleteDelete multiple redirects
GET/admin/redirects/exportExport filtered redirects as CSV
POST/admin/redirects/importImport redirects from CSV
POST/admin/redirects/sync-cloudflareManual Cloudflare sync

Configuration

Plugin Settings

SettingTypeDefaultDescription
enabledbooleantrueEnable/disable redirect processing
autoOffloadEnabledbooleanfalseAuto-sync eligible redirects to Cloudflare

Environment Variables

VariableRequiredDescription
CLOUDFLARE_API_TOKENFor Cloudflare syncAPI token with Lists Edit + Rulesets Edit
CLOUDFLARE_ACCOUNT_IDFor Cloudflare syncYour Cloudflare account ID
REDIRECTS_API_KEYFor REST APIBearer token for API authentication

Middleware

The redirect middleware processes incoming requests with sub-millisecond lookups:

  1. Normalizes the URL (lowercase, strips trailing slash)
  2. Checks the LRU cache (capacity: 1000 entries)
  3. Falls back to database on cache miss
  4. Records hit analytics asynchronously
  5. Executes the redirect

The cache is automatically invalidated on create, update, and delete operations.


Next Steps

Was this page helpful?