How to Set Up 301 Redirects With .htaccess (and Nginx)

Learn when to use 301 or 302 redirects, how .htaccess rewrite rules work, and how to force HTTPS, pick www or non-www and move domains without losing traffic.

Updated September 29, 2026 5 min read

Use the free .htaccess Redirect GeneratorGenerate 301/302 redirects for Apache and Nginx

URLs change for good reasons: a site redesign renames pages, a business rebrands to a new domain, a blog drops dates from its addresses, or you finally install an SSL certificate and want every visitor on HTTPS. Each change leaves old links in emails, bookmarks and search results. Without redirects, those links end in a 404 page, and the search visibility the old URLs earned is lost.

This guide explains the difference between redirect types, how Apache's .htaccess rules and their Nginx equivalents work, and how to generate a correct set of rules for common situations. It finishes with the problems that most often break redirects, including loops and caching.

Redirect status codes in brief

Code Meaning Use it when
301 Moved permanently The old URL is gone for good (renamed pages, domain moves, HTTPS)
302 Found (temporary) The change is short-lived, such as a seasonal page or maintenance
307 / 308 Temporary / permanent, keeping the request method APIs and forms where a POST must stay a POST

Search engines treat a permanent redirect as a strong signal to replace the old URL with the new one in their index. A temporary redirect tells them to keep the old URL. Choosing 302 for a permanent move is a common mistake that delays that consolidation. You can look up any status code in the HTTP status codes reference.

How .htaccess redirects work

.htaccess is a configuration file that Apache reads from each directory on every request. Redirects usually use the mod_rewrite module:

<IfModule mod_rewrite.c>
RewriteEngine On

# HTTP -> HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

# Single page
RewriteRule ^old-page/?$ /new-page [R=301,L]
</IfModule>
  • RewriteCond lines are conditions that must be true for the next rule.
  • RewriteRule has a pattern (a regular expression matched against the path, without the leading slash), a target, and flags.
  • [R=301,L] means "redirect with status 301, and stop processing further rules".

Order matters. Site-wide rules such as HTTPS and www handling normally come first, so that a request is fixed in one hop where possible.

Nginx does not read .htaccess files at all. The same logic lives in the server configuration, usually with return 301 inside server blocks, which is simpler and faster than rewrite rules.

Generate redirect rules

The .htaccess Redirect Generator produces Apache rules and matching Nginx configuration from a short form.

  1. Open the .htaccess Redirect Generator and enter Your domain, for example example.com.
  2. Choose the Site-wide redirect type: 301 Permanent or 302 Temporary.
  3. Under Site-wide rules, switch on Force HTTPS (HTTP → HTTPS) if your certificate is installed. Choose www handling (Leave as is, www → non-www or non-www → www) and a Trailing slash policy (Leave as is, Always add / or Always remove /).
  4. For a rebrand, switch on Redirect an old domain to your domain under Whole domain move and enter the Old domain. Every path is kept, so old-domain.com/pricing goes to example.com/pricing.
  5. Under Single page redirects, click Add redirect for each moved page. Enter the old path, the new path or full URL, and choose 301 or 302 for that row. Old paths match with or without a trailing slash, and query strings are passed through unchanged.
  6. Switch between the Apache .htaccess and Nginx tabs, then copy or download the output. Start over clears the form.

For Apache, add the rules near the top of the .htaccess file in your site root. For Nginx, merge the server blocks into your configuration, add your SSL certificate lines, then run nginx -t to test and reload.

Other ways to set up redirects

Hosting control panels

cPanel and similar panels have a Redirects page that writes .htaccess rules for you. It is convenient for a handful of pages, but the generated rules can be harder to read and maintain in bulk.

CMS plugins

WordPress redirection plugins store redirects in the database and log 404 errors so you can spot missing redirects. They are the easiest option for non-technical editors, at the cost of running PHP for every redirect.

CDN and edge rules

Cloudflare, Netlify, Vercel and other platforms offer redirect rules or _redirects files that run before requests reach your server. For large sites, or hosting where you can't edit server files, these are often the best option.

Server configuration

If you control an Apache server, putting rules in the main configuration instead of .htaccess is more efficient, because Apache does not need to check for .htaccess files on every request.

Troubleshooting redirects

Redirect loops

"Too many redirects" usually means two rules undo each other, for example one adding a trailing slash and another removing it, or HTTPS being forced both in .htaccess and at a CDN that talks to your server over HTTP. Check each layer and keep the logic in one place.

Nothing happens

Confirm that mod_rewrite is enabled and that the server allows overrides for your directory. On shared hosting this is usually on. Also make sure the file is really named .htaccess, with the leading dot and no .txt extension.

Browsers remember 301s

Browsers cache permanent redirects aggressively. While testing, use 302, a private window, or a command-line check such as curl -I https://example.com/old-page to see the real response.

Chains

Avoid redirecting A to B and then B to C. Point every old URL directly at its final destination, and update internal links so visitors don't hit redirects at all.

Special characters in URLs

Paths with spaces or non-ASCII characters must match the encoded form the server receives. A URL encoder shows that form, and the URL parser breaks a full address into host, path and query so you can see exactly what to match.

Crawling rules after a move

After a domain move, keep the old domain's redirects in place for a long time, and do not block the old URLs in robots.txt, or crawlers cannot see the redirects. See the robots.txt generator for the rules on the new site.

Frequently asked questions

Is a 301 redirect bad for SEO?

No. A permanent redirect is the recommended way to move a page. Search engines generally pass the old page's signals to the new URL once they process it.

How long should I keep redirects?

Keep them as long as possible, ideally permanently. Old links in emails, documents and other websites can keep sending visitors for years.

Can I use .htaccess on Nginx?

No. Nginx ignores .htaccess files. The equivalent rules must be added to the Nginx server configuration.

Should I redirect www to non-www or the other way round?

Either works. What matters is choosing one version, redirecting the other to it, and using the same version in canonical tags and sitemaps.

Why does my old page still appear in Google?

Search engines need to recrawl the old URL to see the redirect. It can take some time, especially for pages that are rarely crawled.

Try it now: .htaccess Redirect Generator

Loading tool…