.htaccess Redirect Generator
Site-wide rules
Page redirects
FREE TOOL ยท NO SIGN-UP
This htaccess redirect generator writes Apache mod_rewrite rules for the redirects most sites need. Set site-wide rules to force HTTPS, choose www or non-www, add or remove trailing slashes and strip index.html or index.php. Then add page redirects one by one as 301, 302, 307 or 410, or paste old and new URLs in bulk. Click Generate .htaccess to get a commented, ready-to-paste block, and copy or download it for your server.
What the .htaccess File Does on Apache
An .htaccess file is a directory-level config file for the Apache HTTP Server. Apache reads it on every request and applies its rules to that folder and everything below it. The file sits in the root directory of your site, usually public_html, and its name starts with a dot, so many file managers hide it by default.
It can control redirects, URL rewriting, access rules, caching headers, custom error pages and more. For SEO, redirects are the main use: sending visitors and search engines from old URLs to new ones, and making sure every page loads on one canonical domain.
The redirect engine behind most rules is mod_rewrite. It reads conditions (RewriteCond) and rules (RewriteRule), matches the requested URL against regex patterns, and sends a redirect or rewrites the request.
.htaccess works on Apache and on LiteSpeed, which reads the same format. Many shared hosts using cPanel run one of these. WordPress also uses .htaccess for its pretty permalinks, which is why the file usually already exists on WordPress sites.
Redirect Types the Generator Supports
The generator handles two kinds of rules. Site-wide rules apply to every request: force HTTPS, set the preferred domain, control trailing slashes and remove index files. Page redirects map one old path to one new URL.
Each page redirect can be:
- 301: permanent redirect
- 302: temporary redirect
- 307: temporary redirect that keeps the request method
- 410: gone, for pages removed on purpose with no replacement
Bulk pairs pasted into the text box are always created as 301 redirects.
301 permanent versus 302 temporary redirects
A 301 redirect tells browsers and search engines the page has moved for good. Google treats it as a strong signal to replace the old URL with the new one in its index and to pass ranking signals, often called link equity, to the new address. Use 301s for site migrations, changed slugs, merged articles and domain changes.
A 302 redirect says the move is temporary and the old URL will come back. Search engines tend to keep the original URL indexed. Use it for short-term promotions, maintenance pages or A/B tests. A 307 is similar but guarantees the browser repeats the same request method, which matters mainly for forms.
If a “temporary” redirect stays in place for months, Google may eventually treat it as permanent. Pick the code that matches your real intent.
Forcing HTTPS on every request
Once your site has an SSL certificate, every HTTP request should redirect to HTTPS. Otherwise both versions may be crawled and indexed, splitting signals and showing “Not secure” warnings in browsers.
The “Force HTTPS” option, on by default, adds a rule that checks whether the request came in over HTTP and, if so, sends a 301 redirect to the same host and path on HTTPS. Install and test your SSL certificate before turning this on, or visitors will hit certificate errors.
Choosing www or non-www as the canonical domain
www.example.com and example.com are technically different hosts. Pick one as your canonical domain and redirect the other to it. Neither is better for SEO; consistency is what matters.
The generator defaults to “Force non-www”. You can switch to “Force www” or “Leave as is” if your host or CDN already handles it. Both domain rules redirect to the HTTPS version, so use them on sites that already have SSL working on both hostnames. Make sure your canonical tags, sitemap and internal links all use the same version.
How to Generate and Install Redirect Rules
- Choose site-wide rules. Set Force HTTPS, Preferred domain, Trailing slash (add, remove or leave as is) and index.html / index.php (redirect to the folder or leave as is).
- Add page redirects. Enter an old path like
/old-page/and a new URL likehttps://example.com/new-page/, then pick the status code. Click “+ Add redirect” for more rows. You can paste a full old URL; the tool uses only its path. - Paste bulk pairs (optional). Put one pair per line, old and new separated by a space. Each becomes a 301.
- Click Generate .htaccess. The output is wrapped in
<IfModule mod_rewrite.c>withRewriteEngine Onand a comment above each block. The tool warns you if a target is neither a full URL nor a path starting with/. - Back up your current file. Download a copy of the existing
.htaccessbefore you change anything. - Paste the rules. Open
.htaccessin your host’s file manager or over SFTP. Place the new block near the top, above the# BEGIN WordPresssection on WordPress sites. - Save and test the old URLs straight away.
The generator runs 100% in your browser, and nothing you enter is uploaded. Use Copy, or Download to save the rules as a text file.
How mod_rewrite Rules Are Read
Apache processes .htaccess rules from top to bottom. Each RewriteRule has three parts: a regex pattern that matches the URL path, a target, and flags in square brackets.
Flags change behavior. [R=301] sends a redirect with that status code. [L] means “last”: stop processing further rules for this pass. [NC] makes a match case-insensitive, and [G] returns 410 Gone.
RewriteCond lines sit directly above a rule and must all be true for that rule to run. For example, the HTTPS rule only fires when %{HTTPS} is off.
Order matters. Put site-wide rules first and specific page redirects after, as the generator does. Note that the pattern in a RewriteRule doesn’t see the query string, so a rule for /page/ also catches /page/?ref=abc. Matching on query strings needs a custom RewriteCond.
Redirects During a Site Migration
Redirects are the backbone of any URL migration or domain change. Before you move, build a full redirect map: every old URL that has traffic, backlinks or rankings, paired with its closest new equivalent.
Some rules of thumb:
- Redirect one-to-one where possible. Send each old page to its most relevant new page, not everything to the homepage.
- Avoid redirect chains. Old URL to new URL in one hop, not A to B to C.
- Use 410 for content you removed on purpose with no replacement.
- Keep redirects in place long term. Google recommends at least a year, and links pointing to old URLs may last much longer.
The bulk pairs box is built for migrations. Export your old-to-new map from a spreadsheet as two space-separated columns and paste it in.
Errors That Break .htaccess Files
- Syntax errors. One typo can cause a 500 internal server error for the whole site. Always keep a backup so you can restore it in seconds.
- Redirect loops. Conflicting rules, such as a plugin forcing www while
.htaccessforces non-www, send browsers round in circles. - Rules in the wrong order. Rules placed below the WordPress block may never run.
- mod_rewrite not enabled. The
<IfModule>wrapper prevents errors, but the rules then do nothing. Ask your host to enable it. - Relative targets without a slash. Targets must be full URLs or start with
/. - Redirecting everything to the homepage. Google may treat mass homepage redirects as soft 404s.
- Forgetting the CDN or proxy. Behind Cloudflare or a load balancer, the HTTPS check may need a different condition.
- Cache confusion. Browsers cache 301s aggressively. Test in a private window.
When Server Config or a Plugin Is Better
.htaccess is convenient, but not always the best choice. Apache reads it on every request, which adds a small overhead. On servers where you control the main config, putting redirects in httpd.conf or a virtual host file is faster and cleaner.
Nginx doesn’t read .htaccess at all. Use the Nginx Redirect Generator for Nginx servers.
On WordPress, a redirect plugin or the redirect manager in Rank Math is often easier for one-off redirects, especially if non-technical editors need to manage them. Keep site-wide rules like HTTPS and www in .htaccess or server config, and avoid duplicating the same rule in both places.
Testing Redirects After Upload
After saving the file, test immediately:
- Open your old URLs in a private browser window.
- Check HTTP and HTTPS, www and non-www versions of the homepage.
- Use a redirect checker or your browser’s developer tools (Network tab) to confirm the status code and that each URL takes one hop.
- Watch Google Search Console’s Page indexing report over the following weeks for 404s and redirect errors.
Backing Up Before Every Rule Change
Download your current .htaccess before every edit, change one thing at a time, and test right away. Plan crawl rules with the Robots.txt Generator, create clean new URLs with the URL Slug Generator, write Nginx rules with the Nginx Redirect Generator, and find more on the FHSEO Hub free SEO tools page.
