An .htaccess redirect generator writes the Apache rules that send visitors and search engines from an old address to a new one. Choose what you want to redirect, fill in the old and new addresses, pick the redirect type and copy the finished lines. It covers a single page, a pattern of pages, a removed page, a whole domain, the www and non-www versions of a site and the move from HTTP to HTTPS.
The rules use the standard Apache directives Redirect and RedirectMatch from mod_alias [1] and RewriteCond and RewriteRule from mod_rewrite [2]. They work on Apache servers where .htaccess files are allowed, which is common on shared hosting.
How to use the .htaccess redirect generator
- Pick what to redirect: one page, a pattern, a removed page, a whole domain, www or non-www, or HTTP to HTTPS.
- Pick the redirect type: 301 for a permanent move, 302 for a temporary one. 307 and 308 do the same jobs but keep the request method.
- Fill in the boxes that appear. Old paths start with a slash, new addresses are full URLs or paths, and domains are written without https://.
- Press Generate and copy the lines.
- Paste them into the .htaccess file in your site’s root folder, then test the old address in a private window.
Which mode to choose
| Mode | Apache directive | Use it when |
|---|---|---|
| One page | Redirect | A page or folder moved. By default the path and everything below it is matched. |
| One page, exact match | RedirectMatch | Only that exact page should move, not the pages below it. |
| Pattern | RedirectMatch | Many pages move by a rule, for example /blog/ to /news/. |
| Removed page | Redirect gone | A page is permanently removed and has no replacement (status 410). |
| Whole domain | RewriteCond and RewriteRule | The site moves to a new domain and every path should follow. |
| www or non-www | RewriteCond and RewriteRule | You want one canonical host name. |
| HTTP to HTTPS | RewriteCond and RewriteRule | All traffic should use a secure connection. |
How the Redirect directive behaves
The Redirect directive matches a URL path by its beginning, and the rest of the path and the query string are added to the new address [1]. Matching is by whole path segment, so /service does not match /servicefoo.txt. If you give no status, the redirect is temporary, a 302 [1], so always choose the type on purpose. POST data is discarded in a redirect, which is why the 307 and 308 codes exist.
RedirectMatch works like Redirect but takes a regular expression, and the captured groups can be used in the new address as $1, $2 and so on [1]. Redirects of this kind are processed before any Alias, and the first match wins, so put the most specific paths first.
Permanent or temporary?
A permanent redirect, 301 or 308, says the new address replaces the old one, and the temporary codes, 302 and 307, say the old address will come back [3]. The difference between the pairs is that 307 and 308 keep the request method and body when the request is repeated. Use a permanent redirect for a real move and a temporary one for maintenance pages or tests.
Worked example: moving a site to a new domain
You are moving old-site.com to new-site.com and want every path to keep working. Choose Whole domain, enter old-site.com and new-site.com, keep 301 and press Generate. The result starts with RewriteEngine on, adds a condition that the host is old-site.com or www.old-site.com, and ends with a rule that sends every path to https://new-site.com with the same path, using the flags R=301 and L [2]. The L flag stops processing so that no later rule changes the result.
Forcing HTTPS and one host name
The HTTPS rule checks the server variable %{HTTPS}, which is on when the connection uses SSL or TLS and off otherwise [2], and redirects when it is off. Pick either the www or the non-www form of your host name and redirect the other one to it, so search engines see one address for each page. The NC flag in the generated conditions makes the host check case-insensitive.
Testing your redirects
After you upload the file, open the old address in a private window, so that no cached redirect interferes, and check that you land on the new address. Browser developer tools show the status code in the network panel: you should see the code you chose, followed by the request for the new address. Test a few deeper paths and an address with a query string, to confirm that the rest of the path is carried over.
Start with a temporary 302 while you test and switch to 301 only when the rule is right, because browsers and search engines may remember a permanent redirect for a long time. Keep a copy of the old .htaccess file so that you can go back, and add your redirects above general rules such as those of a CMS, since rules are applied in order.
- Check the www and non-www versions and the HTTP and HTTPS versions of the old address.
- Redirect each old page to the most relevant new page instead of sending everything to the home page.
- Fix internal links so that they point to the new address and do not rely on the redirect.
Common mistakes
- Putting the rules in the wrong folder: .htaccess works for the folder it sits in and the folders below.
- Creating redirect loops, for example redirecting to a path that matches the same rule again.
- Using a temporary redirect for a permanent move, so the new page gets less credit.
- Forgetting that Redirect matches everything below the path unless you choose the exact match.
- Testing only in a browser that cached an earlier permanent redirect.
This tool writes rules for Apache. Nginx and other servers use different syntax, and some hosts do not allow .htaccess files. Back up your .htaccess before you change it, because a mistake can take the site offline.
What next
Use this often? Add .htaccess Re to your Home screen.
Frequently asked questions
What does an .htaccess redirect generator do?
It writes the Apache lines that redirect one address to another, so you can copy them into your .htaccess file instead of writing the syntax by hand.
Which redirect type should I choose?
Use 301 for a permanent move and 302 for a temporary one. 308 and 307 are the same with one difference: they keep the request method and body when the request is repeated.
Where do I put the generated rules?
In the .htaccess file in your site’s root folder, above other rules. Back up the file first and test the old address afterwards.
Does Redirect match pages below the path?
Yes. Redirect matches the path and everything below it. Choose the exact match option when only one page should move.
Will this work on Nginx?
No. The rules are for Apache. Nginx uses a different syntax in its server configuration.
Sources
Was this tool helpful?
What went wrong?
We only receive the reason you pick, never what you typed into the tool.
Thanks for your feedback.