1001Ferramentas
↪️Generators

.htaccess Redirect Generator

Generate Apache redirect rules for .htaccess with 301 (permanent) or 302 (temporary). Supports multiple rules.

Cole pares "origem destino" separados por espaço, um por linha:


  

Apache .htaccess redirects in depth: 301 vs 302, mod_alias vs mod_rewrite, and SEO migration

Redirects in Apache via .htaccess are one of the most common operations during a website's lifecycle: SEO migration after a CMS swap, moving from HTTP to HTTPS, consolidating www and apex domains, retiring URLs, normalizing trailing slashes, or shipping a brand-new domain without losing PageRank. Apache offers three different mechanisms to express a redirect, and choosing the right one — together with the correct HTTP status code — directly affects how browsers cache the response and how search engines transfer link equity.

This generator emits Redirect directives for the pairs you paste; the reference below covers the three approaches, every status code you may need, HTTPS and canonical-host patterns, performance gotchas of .htaccess overrides, and how to debug a redirect loop in production.

Three ways to write a redirect

  • Redirect (mod_alias) — simplest. Redirect 301 /old-page /new-page. Matches an exact path prefix and emits a Location header. No regex.
  • RedirectMatch (mod_alias) — regex. RedirectMatch 301 ^/products/old/(.*)$ /products/new/$1. Ideal for bulk redirects with capture groups.
  • RewriteRule (mod_rewrite) — most powerful. Supports conditions (RewriteCond), flags, and access to request variables. Use it whenever the redirect depends on the host, scheme, user agent, or any header.

HTTP status codes for redirects

  • 301 Moved Permanently — browsers cache forever, search engines transfer PageRank. Default for SEO migrations.
  • 302 Found — temporary, not cached. Use during A/B tests or short-lived maintenance.
  • 303 See Other — after a POST, forces the client to GET the new URL (Post-Redirect-Get pattern).
  • 307 Temporary Redirect — like 302 but preserves the HTTP method (POST stays POST).
  • 308 Permanent Redirect — modern successor of 301; preserves the method. Pick 308 over 301 only if you need POST/PUT to be repeated on the new URL.

HTTPS, www canonical, and trailing slash patterns

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

# Drop www
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule (.*) https://example.com/$1 [R=301,L]

# Add trailing slash (avoid loops with REQUEST_FILENAME tests)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]

When chaining redirects, Apache aborts after roughly 10 internal redirects to protect against loops. Always confirm headers with curl -I https://example.com/old-page — you should see a single 301 hop, not a chain of 301 → 301 → 200.

Performance and debugging

Apache reads every .htaccess on every request from the URL's directory up to the document root, so heavy override files add latency. When you control the virtual host config, move the rules to <VirtualHost> with AllowOverride None for a measurable speedup. For debugging, raise LogLevel rewrite:trace3 temporarily — the access log will print each rule the request matched. The legacy RewriteLog directive was removed in Apache 2.4.

SEO migration checklist

  • Use 301 (or 308) so search engines transfer ranking signals.
  • Map old URLs page-to-page to the closest equivalent. Bulk-redirecting everything to the home page wastes the link equity you spent years earning.
  • Keep redirects live for at least 12 months — Google may keep re-crawling old URLs for that long.
  • Update internal links to point straight at the new URLs; redirects should catch external traffic, not your own navigation.
  • Resubmit the new sitemap in Search Console and monitor the "Coverage" report for crawl errors.

FAQ

What is the difference between 301 and 302? 301 says "this resource moved permanently" — browsers cache the new URL aggressively and search engines transfer PageRank. 302 is temporary, not cached, and does not pass ranking signals. Use 301 for migrations, 302 only for short A/B tests.

How do I bulk-redirect hundreds of URLs? Either paste every pair into this generator (one per line) or use RedirectMatch with a regex when the URLs follow a pattern. For very long lists, a RewriteMap backed by a text file is faster because Apache hashes it in memory.

How do I verify a redirect is actually returning 301? Run curl -I -L https://example.com/old-page. The first response should be HTTP/1.1 301 followed by Location:. If you see 302 or a chain of multiple 301s, fix the rule.

Why am I getting a redirect loop? The destination matches the same rule and bounces back. Add a RewriteCond to skip already-rewritten requests, e.g. RewriteCond %{ENV:REDIRECT_STATUS} ^$, or guard the source pattern so it cannot match the destination.

Related Tools