Redirect Rules (redirects.txt)
Redirect rules help you manage URL changes, handle Single Page Applications (SPAs), and create a better user experience by directing visitors to the right content.
Quick Start
- Create a file named
redirects.txtin your project's root folder - Add your redirect rules (one per line)
- Publish your project
Each rule follows this format:
/source-path /destination-path [status-code][!]
Example:
/pricing /plans
When someone visits /pricing, they'll be automatically redirected to /plans.
Serve Directory
The serve directory feature lets you serve files from a specific folder instead of the project root.
Syntax:
serve = "/foldername"
What it does:
When you set serve = "/public", all your files are served from the /public folder. For example:
- Visitor requests:
https://username.srcnexus.app/projectname/about - Server looks for:
/public/about(instead of/about) - Visitor requests:
https://username.srcnexus.app/projectname/ - Server serves:
/public/index.html(instead of/index.html)
How to change the serve directory:
Option 1: Via redirects.txt file
Add this line at the top of your redirects.txt:
serve = "/foldername"
Option 2: Via Editor UI
- Open your project in the editor
- Click the three dots menu in the top right corner
- Select Configure Serve Directory
- Enter your folder name
- Publish your project for changes to take effect
Basic Redirects
Permanent Redirect (Default)
/contact-us /contact
This is a 301 redirect - it tells browsers and search engines that the page has permanently moved. Good for SEO!
When to use: You've renamed a page and want search engines to update their indexes.
Temporary Redirect
/maintenance /coming-soon 302
This is a 302 redirect - it tells browsers the page is temporarily at a different location.
When to use: You're doing maintenance or A/B testing, and the page might move back.
Pattern Matching
Wildcard Redirects (Splat)
Redirect entire sections of your site:
/blog/* /articles/:splat
What happens:
/blog/my-first-post→/articles/my-first-post/blog/2024/tutorial→/articles/2024/tutorial
The * captures everything after /blog/ and :splat puts it in the new path.
Real example: You renamed your blog folder from "posts" to "writings":
/posts/* /writings/:splat
Placeholder Matching
Extract specific parts of a URL:
/user/:username /profile/:username
What happens:
/user/john→/profile/john/user/sarah→/profile/sarah
Each :name captures that segment of the URL.
Real example: Old URL structure to new structure:
/products/:category/:id /shop/:category/item/:id
/products/electronics/laptop-123→/shop/electronics/item/laptop-123
Query Parameter Matching
Redirect based on URL parameters:
/search q=:query /results/:query 301
What happens:
/search?q=javascript→/results/javascript/search?q=react→/results/react
Real example: Old search URL to new format:
/product id=:productId /item/:productId
/product?id=abc123→/item/abc123
Rewrites (Status 200)
Serve different content without changing the browser's URL. Perfect for Single Page Applications!
/* /index.html 200
What happens:
- Visitor types:
yoursite.com/about - Browser shows:
yoursite.com/about(stays the same) - Server serves:
/index.html(your SPA handles routing)
Real example: React/Vue/Angular app:
# Let your SPA handle all routes
/* /index.html 200
Force Redirects
Use ! to redirect even if the file exists:
/old-design.html /new-design.html 301!
Without !: If /old-design.html exists, it would be served (no redirect)
With !: Always redirects to /new-design.html, even if the old file exists
When to use: You want to ensure old URLs always redirect, regardless of what files exist.
Comments
Add notes to document your rules:
# Updated pricing page URL (Dec 2024)
/old-pricing /pricing
# Redirect legacy blog posts
/articles/* /blog/:splat
Lines starting with # are ignored and won't be uploaded to the server.
[!IMPORTANT] > Comment lines don't count toward the 3000 character limit! They're automatically removed when you publish your project, so feel free to add as many helpful notes as you need.
Rule Processing Order
Critical: Rules are checked from top to bottom. The first matching rule wins.
✅ Correct Order
/blog/featured-post /special-feature
/blog/* /articles/:splat
/* /index.html 200
Why this works:
/blog/featured-postis checked first (exact match)/blog/*catches other blog posts/*catches everything else
❌ Wrong Order
/* /index.html 200
/blog/featured-post /special-feature
Why this fails:
/*matches everything, so/blog/featured-postis never checked!
Rule of thumb: Put specific rules before wildcard rules.
Common Examples
Single Page Application
# Serve index.html for all routes (React/Vue/Angular)
/* /index.html 200
Your JavaScript framework handles the actual routing.
Website Restructuring
Old structure: /pages/about.html
New structure: /about
# Remove /pages/ prefix and .html extension
/pages/about.html /about
/pages/contact.html /contact
/pages/team.html /team
Blog Migration
Old blog: /wordpress/2024/01/post-title
New blog: /blog/post-title
# Flatten URL structure
/wordpress/:year/:month/:slug /blog/:slug
E-commerce URL Cleanup
# Simplify product URLs
/products/view id=:id /product/:id
# Cart with old parameter format
/shopping-cart item=:item /cart/:item
Catch-All 404
# Specific redirects first
/old-page /new-page
# Catch everything else and show 404
/* /404.html 404
Complete Real-World Example
Here's a complete redirects.txt for a business website:
# Homepage redirect from old domain structure
/index.html /
/home /
# Pricing page was renamed
/pricing-plans /pricing
/old-pricing /pricing
# Blog moved from /posts to /blog
/posts/* /blog/:splat
# Product pages restructured
/products/:category/:id /shop/:category/:id
# Old contact form
/contact-us /contact
# Services section renamed
/our-services/* /services/:splat
# Force redirect for rebranded page
/company-info /about 301!
# Single Page App catch-all (must be last!)
/* /index.html 200
Tips for Success
- Test your redirects - Visit old URLs after publishing to verify they work
- Order matters - Always put specific rules before wildcard rules
- Use 301 for SEO - Permanent redirects help search engines update their index
- Use 200 for SPAs - Keeps the URL clean while serving your app
- Document with comments - Future you will appreciate the notes!
- Check for loops - Make sure you're not redirecting
/pageto/page
Limitations
- Maximum 3000 characters for all redirect rules (comments excluded)
- Wildcards (
*) only work at the end of a path - Query parameters require exact parameter names
File Existence Behavior
By default, if a file exists at the source path, it will be served (redirect is skipped).
Example:
Rule: /about /about-us
File exists: /about.html
Result: Serves /about.html (no redirect)
To force redirect anyway, use the ! flag:
/about /about-us 301!
Changes to redirects.txt take effect immediately after you publish your project.