Redirect Rule Generator

Generate simple redirect rules for Apache and Nginx.

Back to all tools on ToolForge

More in SEO Tools

Redirect Type:

From Path:

To URL:

About Redirect Rule Generator

This tool generates HTTP redirect rules for Apache and Nginx web servers. It creates properly formatted redirect directives using the specified source path and target URL. The tool supports both 301 (permanent) and 302 (temporary) redirect status codes and escapes special regex characters in paths.

It is useful for site migrations, URL structure updates, removing or consolidating pages, fixing broken links, redirecting www to non-www (or vice versa), enforcing HTTPS, and redirecting deleted content to relevant alternatives.

HTTP Redirect Status Codes

HTTP redirects use 3xx status codes to indicate URL changes:

Common Redirect Status Codes:

301 Moved Permanently
  - Indicates permanent URL change
  - Search engines update index to new URL
  - SEO value (link equity) is transferred
  - Browser may cache the redirect
  - Use for: site migrations, permanent moves

302 Found (Temporary Redirect)
  - Indicates temporary URL change
  - Search engines keep original URL indexed
  - SEO value stays with original URL
  - Browser requests original URL next time
  - Use for: maintenance, A/B testing, temporary moves

307 Temporary Redirect
  - Like 302 but preserves HTTP method
  - POST requests remain POST (not GET)
  - HTTP/1.1 replacement for 302
  - Use for: temporary API redirects

308 Permanent Redirect
  - Like 301 but preserves HTTP method
  - HTTP/1.1 replacement for 301
  - Use for: permanent API endpoint moves

Less Common:
  300 Multiple Choices - Several options available
  303 See Other - Use GET for response
  305 Use Proxy - Deprecated, security concerns

Redirect Type Comparison

Property 301 Permanent 302 Temporary
SEO Value Transfer Yes (most PageRank) No
Search Engine Index Updates to new URL Keeps original URL
Browser Caching May cache permanently Does not cache
Use Case Permanent moves Temporary changes
Example Scenario Site migration Maintenance page
Change Back Later Not recommended Expected

Apache Redirect Syntax

Apache Redirect Directives:

Basic Redirect (mod_alias):
  Redirect 301 /old-page https://example.com/new-page
  Redirect 302 /temp https://example.com/alternate

  Syntax: Redirect [status] [URI-path] [target-URL]

Redirect with Regex (mod_rewrite):
  RewriteEngine On
  RewriteRule ^old-page$ https://example.com/new-page [R=301,L]

  RewriteRule ^products/(.*)$ https://example.com/items/$1 [R=301,L]

  Syntax: RewriteRule [pattern] [substitution] [flags]

Common Flags:
  [R=301]     - 301 permanent redirect
  [R=302]     - 302 temporary redirect
  [L]         - Last rule (stop processing)
  [NC]        - No case (case-insensitive)
  [QSA]       - Query string append

Redirect www to non-www:
  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
  RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

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

Redirect old domain to new domain:
  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^olddomain\.com [NC,OR]
  RewriteCond %{HTTP_HOST} ^www\.olddomain\.com [NC]
  RewriteRule ^(.*)$ https://newdomain.com/$1 [R=301,L]

Nginx Redirect Syntax

Nginx Redirect Directives:

Using rewrite directive:
  rewrite ^/old-page$ https://example.com/new-page permanent;
  rewrite ^/temp$ https://example.com/alternate redirect;

  Syntax: rewrite [regex] [replacement] [flag];

  Flags:
    permanent - 301 Moved Permanently
    redirect  - 302 Found
    break     - Stop processing rewrite
    last      - Continue with next location

Using return directive (simpler):
  location /old-page {
    return 301 https://example.com/new-page;
  }

  location /temp {
    return 302 https://example.com/alternate;
  }

Redirect www to non-www:
  server {
    listen 80;
    server_name www.example.com;
    return 301 https://example.com$request_uri;
  }

Redirect HTTP to HTTPS:
  server {
    listen 80;
    server_name example.com;
    return 301 https://$server_name$request_uri;
  }

Redirect with regex capture:
  location ~ ^/products/(.*)$ {
    return 301 https://example.com/items/$1;
  }

Redirect entire directory:
  location /old-folder/ {
    return 301 https://example.com/new-folder/$request_uri;
  }

Common Use Cases

Scenario Redirect Type Example
Site Migration 301 olddomain.com → newdomain.com
URL Structure Change 301 /blog/post-name → /articles/post-name
Page Consolidation 301 /page1, /page2 → /main-page
Deleted Content 301 /old-product → /products-category
Maintenance Mode 302 /* → /maintenance.html
A/B Testing 302 /landing → /landing-variant-a
WWW Enforcement 301 www.example.com → example.com
HTTPS Enforcement 301 http:// → https://

Redirect Rule Examples

Example 1: Simple Page Move
  From: /old-page
  To:   https://example.com/new-page

  Apache: Redirect 301 /old-page https://example.com/new-page
  Nginx:  rewrite ^/old-page$ https://example.com/new-page permanent;

Example 2: Blog URL Restructure
  From: /blog/post-title
  To:   https://example.com/articles/post-title

  Apache: Redirect 301 /blog/post-title https://example.com/articles/post-title
  Nginx:  rewrite ^/blog/post-title$ https://example.com/articles/post-title permanent;

Example 3: Product Category Move
  From: /products/electronics/phones
  To:   https://example.com/phones

  Apache: Redirect 301 /products/electronics/phones https://example.com/phones
  Nginx:  rewrite ^/products/electronics/phones$ https://example.com/phones permanent;

Example 4: Temporary Maintenance
  From: /checkout
  To:   https://example.com/maintenance

  Apache: Redirect 302 /checkout https://example.com/maintenance
  Nginx:  rewrite ^/checkout$ https://example.com/maintenance redirect;

Example 5: With Query String
  From: /search?q=hello
  To:   https://example.com/search/hello

  Apache: RewriteRule ^search$ https://example.com/search/%1 [R=301,L]
  Nginx:  rewrite ^/search\?q=(.*)$ https://example.com/search/$1 permanent;

Example 6: Domain Migration
  From: /
  To:   https://newdomain.com/

  Apache: RedirectMatch 301 ^(.*)$ https://newdomain.com$1
  Nginx:  rewrite ^(.*)$ https://newdomain.com$1 permanent;

Redirect Best Practices

Common Redirect Mistakes

Testing Redirects

Testing Methods:

1. cURL Command Line:
   curl -I https://example.com/old-page

   Response headers show:
   HTTP/1.1 301 Moved Permanently
   Location: https://example.com/new-page

2. Browser DevTools:
   - Open Network tab
   - Navigate to old URL
   - Check status code and Location header

3. Online Tools:
   - httpstatus.io
   - Redirect Path (Chrome extension)
   - WhereGoes.com

4. Google Search Console:
   - Check Coverage report for redirect errors
   - Use URL Inspection tool

Expected Response Headers:
  HTTP/1.1 301 Moved Permanently
  Location: https://example.com/new-page
  Cache-Control: no-cache

  HTTP/1.1 302 Found
  Location: https://example.com/temporary
  Cache-Control: no-store

Frequently Asked Questions

What is the difference between 301 and 302 redirects?
A 301 redirect indicates a permanent move, telling search engines to transfer SEO value and update their index. A 302 redirect indicates a temporary move, keeping the original URL indexed. Use 301 for permanent URL changes, 302 for temporary maintenance or A/B testing.
How do I implement redirects in Apache?
In Apache, add redirect rules to .htaccess or virtual host config using 'Redirect' directive: 'Redirect 301 /old-page https://example.com/new-page'. For regex patterns, use 'RedirectMatch'. Ensure mod_alias or mod_rewrite is enabled.
How do I implement redirects in Nginx?
In Nginx, use 'rewrite' directive with 'permanent' (301) or 'redirect' (302) flag: 'rewrite ^/old-page$ https://example.com/new-page permanent;'. Alternatively, use 'return' directive: 'return 301 https://example.com/new-page;'.
When should I use redirects?
Common scenarios include: site migrations (domain changes), URL structure updates, removing or consolidating pages, fixing broken links, redirecting www to non-www (or vice versa), enforcing HTTPS, and redirecting deleted content to relevant alternatives.
What are redirect chains and why should I avoid them?
Redirect chains occur when URL A redirects to B, which redirects to C. They slow page load, dilute SEO value, and may be truncated by browsers. Always redirect directly from source to final destination. Audit existing rules periodically to eliminate chains.
Do redirects affect SEO?
301 redirects pass most SEO value (link equity) to the new URL, but some PageRank may be lost. 302 redirects do not transfer SEO value since they're temporary. Properly implemented 301 redirects are essential for preserving rankings during URL changes.