What This Tool Does

This tool generates correctly-formatted Apache .htaccess redirect rules for the most common URL redirection scenarios: 301/302 single-page redirects, domain migration, www to non-www (or vice versa), HTTP to HTTPS enforcement, trailing slash normalization, and old-URL-pattern to new-URL-pattern. For each rule, it also shows the equivalent Nginx configuration so you can deploy to any web server.

Inputs Explained

How It Works

Each redirect type maps to specific Apache mod_rewrite directives. The tool assembles correct RewriteRule patterns, RewriteCond conditions, and RedirectMatch directives for your scenario. For Nginx, the equivalent location blocks or rewrite directives are generated — different server but same effect.

Formula / Logic Used

Apache: RewriteEngine On; RewriteCond %{condition}; RewriteRule pattern target [flags] Nginx: location ~ pattern { return 301 target; }

.htaccess Redirect Generator (Apache + Nginx)

Build Apache .htaccess redirect rules with matching Nginx equivalents — no syntax mistakes.

Step-by-Step Example

Example 1 — Single URL redirect (301):

# .htaccess
RewriteEngine On
Redirect 301 /old-page.html /new-page.html

Example 2 — Force HTTPS:

# .htaccess
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Example 3 — Full domain migration (oldsite.com → newsite.com):

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.com$ [NC]
RewriteRule ^(.*)$ https://newsite.com/$1 [R=301,L]

Use Cases

Assumptions and Limitations

Disclaimer: Always test redirects on a staging server first. Incorrect .htaccess rules can create redirect loops or break your entire site. Keep a backup.

Frequently Asked Questions

How do I redirect multiple URLs at once with .htaccess?

For a handful of URLs, just stack Redirect 301 lines: Redirect 301 /old-page-1 /new-page-1 Redirect 301 /old-page-2 /new-page-2 For dozens or hundreds, use RewriteRule with patterns or a RewriteMap (which requires server config access). Our generator lets you paste a CSV of old/new pairs and outputs the entire .htaccess block. Always test in a staging environment first — one wrong rule can take a site down. Order matters too: Apache reads top to bottom, so put more specific rules above general ones. After uploading, check 5-10 URLs manually and watch the access log for 500 errors.

How do I redirect 404 errors to a custom page in .htaccess?

One line: ErrorDocument 404 /404.html. Place it at the top of your .htaccess. The path is relative to your document root, so /404.html maps to yoursite.com/404.html. Make sure the page itself exists and returns a real 404 status code, not 200 — otherwise Google indexes it as a soft-404 and it hurts SEO. You can do the same for other errors: 403 (forbidden), 500 (server error), 503 (maintenance). For dynamic sites, point to a PHP or Node handler instead of a static file. Test by visiting a fake URL and confirming your custom page loads.

How do I test .htaccess redirects without breaking my site?

Three layers of safety. First, test rules locally using a .htaccess on a localhost Apache or in a Docker container — same Apache config, no production risk. Second, use a staging subdomain (staging.yoursite.com) that mirrors production. Third, before deploying, validate the syntax with apachectl -t if you have shell access. Once live, check headers with curl -I https://yoursite.com/old-page — look for 301 Moved Permanently and the correct Location: header. Browser caches can fool you, so use Chrome DevTools' "Disable cache" option or curl for true responses. Always keep a backup copy of the working file.

Why is my .htaccess redirect causing an infinite loop?

This usually means the destination URL also matches the redirect rule, sending the browser back through it forever. Classic example: Redirect 301 / /index.html — every request, including for /index.html, gets redirected to /index.html again. Fix: add a condition that excludes the destination, or use RewriteRule with an anchored pattern like ^old-path$ instead of a loose Redirect. Also check for HTTPS-redirect rules conflicting with HTTP-redirect rules. Browsers stop after about 20 hops and show "ERR_TOO_MANY_REDIRECTS". Our generator outputs anchored, condition-checked rules by default to prevent this exact bug.

Sources and References

Related Calculators

URL EncoderHreflang GeneratorRobots.txt ToolCanonical CheckerRegex TesterMarkdown Converter