.htaccess Redirect Generator
Generate correct .htaccess redirect rules without learning the syntax.
Reviewed by the ToolNestr Editorial Team — July 2026
Select a type, fill in fields, and click Generate rule.
How .htaccess redirects work
Apache's .htaccess (hypertext access) file is a per-directory configuration file supported by Apache-based web servers. When a request arrives, Apache checks the directory and every parent directory for .htaccess files, reading and applying the rules before serving the requested resource. Redirect rules are among the most common directives placed in .htaccess, allowing you to map old URLs to new ones, enforce secure connections, and canonicalise domain variations.
The two main Apache modules used for redirects are mod_alias (providing the Redirect directive) and mod_rewrite (providing RewriteRule). Simple single-URL redirects use Redirect from mod_alias, which is straightforward and efficient. More complex rules — such as HTTP-to-HTTPS enforcement, www canonicalisation, or pattern-based rewrites — require mod_rewrite's RewriteCond / RewriteRule directives, which offer conditional matching with regular expression support.
When Apache encounters a Redirect directive, it responds to the client's request with a 3xx HTTP status code and a Location header pointing to the target URL. The browser then fetches the new URL. Search engines that encounter a 301 response update their index to point to the new location, preserving the old URL's ranking signals. This makes correct redirect configuration essential for any site migration or URL restructuring.
How the rules are built
This generator produces different rule templates depending on the selected redirect type. Each type corresponds to a specific Apache directive or RewriteRule pattern:
Single URL Redirect (301 / 302)
Uses Apache's Redirect directive from mod_alias. Format: Redirect 301 /old-path https://example.com/new-path. The first argument is the status code, the second is the absolute path from the document root, and the third is the full destination URL. This is the simplest and most efficient redirect method for individual URLs.
Force HTTPS
Uses RewriteCond to check if %{HTTPS} is off, then RewriteRule to redirect to the HTTPS version of the same URL. The [R=301,L] flags tell Apache to send a 301 redirect and stop processing further rules. This rule should be placed before any other rules in your .htaccess.
WWW Canonicalisation
Two variants: redirect www to non-www or non-www to www. Both use RewriteCond to match the requested hostname against %{HTTP_HOST}, then issue a 301 redirect to the canonical domain while preserving the request URI via the %{REQUEST_URI} variable. The NC flag makes the hostname comparison case-insensitive.
Worked example
Redirect 301 /blog https://newsite.com/articles
Visitors and search engines arriving at /blog are immediately sent to /articles with a 301 status, preserving SEO ranking.
Common use cases
Migrating URLs without losing SEO
When you restructure your site, old URLs must 301-redirect to new ones. This preserves the ranking signals that search engines have accumulated, preventing traffic loss. A broken redirect means a 404 page, which loses all accumulated SEO value.
Forcing HTTPS encryption
With HTTPS being a ranking signal and a necessity for security, every site should redirect HTTP traffic to HTTPS. The RewriteCond checks if the connection is not already encrypted, then issues a redirect to the HTTPS version of the exact same URL.
Canonical www or non-www
Search engines treat www.example.com and example.com as separate sites. Choosing one canonical form and redirecting the other avoids duplicate content penalties and consolidates ranking signals to a single domain.
Domain migration
When rebranding or moving to a new domain, every URL from the old site must redirect to its corresponding page on the new site. A site-wide 301 from the old domain root can work, but per-URL redirects preserve more SEO value.
Tips for .htaccess redirects
301 = permanent (passes SEO)
Always use 301 for permanent URL changes. Search engines pass approximately 90-99% of page ranking signals through a 301 redirect. A 302 does not pass link equity and should only be used for temporary moves where the old URL will be restored.
Backup .htaccess before editing
A syntax error in .htaccess can bring down your entire site with a 500 Internal Server Error. Always download a backup of your existing .htaccess file before making changes. If something breaks, you can restore the backup immediately via FTP or your hosting control panel.
Test after applying
Use a redirect checker tool to verify that each rule works as expected. Check that old URLs return the correct status code (301/302) and land on the intended destination. Watch for redirect chains or loops that could harm performance and SEO.
What are .htaccess redirects?
An .htaccess redirect is a directive placed in Apache's per-directory configuration file that instructs the web server to respond to requests for a particular URL with a redirect status code and a new location. The file uses the hash (#) character for comments and supports a wide range of directives beyond redirects, including access control, MIME type configuration, and custom error pages.
The two primary modules for redirects are mod_alias (simple, efficient, limited to one-to-one URL mapping) and mod_rewrite (powerful, supports regex, conditional logic, and complex rule chains). For simple redirects, mod_alias is preferred because it has less overhead. mod_rewrite should be used when you need pattern matching, conditional checks (like HTTPS status or hostname), or complex rewriting logic. Both modules are enabled on virtually all Apache installations and shared hosting environments.
Rules in .htaccess are evaluated in order from top to bottom. When a rule matches, Apache applies it and, depending on the flags, may stop processing (L flag) or continue with subsequent rules. This ordering is important: place HTTPS and www canonicalisation rules before individual URL redirects, and place more specific redirects before general ones to avoid conflicts.
Redirect status codes explained
| Code | Name | SEO impact | When to use |
|---|---|---|---|
| 301 | Moved Permanently | Passes link equity | Permanent URL changes, domain migrations |
| 302 | Found | Does not pass equity | Temporary moves, A/B testing, maintenance |
| 307 | Temporary Redirect | Preserves method, no equity | Temporary moves requiring same HTTP method |
| 308 | Permanent Redirect | Passes equity, preserves method | Permanent moves requiring same HTTP method |
Related tools
Frequently asked questions
301 vs 302 — which should I use?
301 (permanent) for real moves so search engines pass ranking; 302 only for temporary changes.
Where does this go?
In the .htaccess file in your site root, usually near the top.
Will it hurt SEO?
A correct 301 preserves SEO value; broken rules or chains can harm it — test after applying.
What if my server uses Nginx?
This generator produces Apache .htaccess rules. Nginx uses a different config format.