URL Redirects
The Ellemment Stack implements redirects using Express middleware, processing incoming requests before they reach Remix for optimal performance.
Security Redirects
HTTP to HTTPS Redirection
Automatic HTTPS enforcement improves security by preventing request interception:
app.use((req, res, next) => { const proto = req.get('X-Forwarded-Proto') const host = getHost(req) if (proto === 'http') { res.set('X-Forwarded-Proto', 'https') res.redirect(`https://${host}${req.originalUrl}`) return } next() })
Note: Local development (localhost) is unaffected, using Fly's request headers for redirect decisions.
URL Normalization
Trailing Slash Removal
Automatically redirect URLs with trailing slashes to their canonical form:
app.use((req, res, next) => { if (req.path.endsWith('/') && req.path.length > 1) { const query = req.url.slice(req.path.length) const safepath = req.path.slice(0, -1).replace(/\/+/g, '/') res.redirect(301, safepath + query) } else { next() } })
Benefits:
- Improves SEO
- Prevents duplicate content issues
- Maintains consistent URLs
- Helps search engine crawlers
Domain Management
Subdomain Configuration
Prerequisites
-
Register SSL certificates for both domains:
- Root domain certificate
- WWW subdomain certificate
- Configure via Fly.io dashboard or CLI
-
Set up DNS records:
- A records
- CNAME records
- SSL verification records
Implementation Options
Option 1: WWW Redirection
Redirect root domain to www subdomain:
app.use((req, res, next) => { const host = getHost(req) if (!host.startsWith('www.')) { return res.redirect(301, `https://www.${host}${req.url}`) } else { next() } })
Option 2: Root Domain Redirection
Redirect www to root domain:
app.use((req, res, next) => { const host = getHost(req) if (host.startsWith('www.')) { return res.redirect(301, `https://${host.slice(4)}${req.url}`) } else { next() } })
Best Practices
Performance
- Early redirection
- Proper status codes
- Cache headers
- Minimal middleware
SEO
- Consistent URLs
- Proper redirects
- Canonical URLs
- Sitemap updates
Security
- HTTPS enforcement
- Secure headers
- SSL certificates
- Domain validation
Implementation Guide
Express Setup
import express from 'express' import { getHost } from './utils' ,[object Object], ,[object Object], ,[object Object],
// Domain management app.use(wwwRedirect)
Middleware Functions
function httpsRedirect(req, res, next) { // HTTPS enforcement implementation } ,[object Object],
function wwwRedirect(req, res, next) { // Domain redirection implementation }
Configuration
// config.ts export const redirectConfig = { enforceHttps: true, removeTrailingSlash: true, preferWww: true, statusCode: 301 }
For more information about security configuration, see the security documentation. For deployment details, refer to the deployment documentation.