SEO · Apache & Nginx

.htaccess and Nginx redirect generator

Force HTTPS, pick www or non-www, fix trailing slashes, and move single pages, whole folders or an entire domain. You get the rules for Apache .htaccess or for Nginx, in a safe order: live as temporary 302s first, checked, then permanent.

Web server
HTTPS & www
Move a whole domain

Every URL on the old domain goes to the same path on the new one, query string included. Put this on the old site. Type the new domain exactly as it should end up (with www if that's your choice), so it's one hop.

Single page redirects
Folder redirects (a folder and everything in it)

Moves the folder and everything under it: /blog → /news, /blog/post → /news/post. Enter / as the new folder to move its pages to the top level.

Options

Warning: one broken line in .htaccess takes the whole site down. Apache reads the file on every request, so a typo gives every page a 500 error at once, and there's no test command.

A 301 is hard to take back, too: browsers remember it even after you remove the rule. Undo: put the backup from step 1 back.

cp .htaccess.bak .htaccess

Put it live safely, in this order

  1. Back up the current config

    cp .htaccess .htaccess.bak

    Run it in the site's root folder. No command line? Download a copy with your FTP program or your host's file manager. Apache never serves files whose name starts with .ht, so the copy stays private. No .htaccess yet? Then the undo is deleting the new one.

  2. Go live with temporary redirects (302)

    # Choose at least one redirect above

    Paste the rules at the top of the .htaccess in your site's root folder, above # BEGIN WordPress if that's there. They work from the next request on.

  3. Check every redirect

    # Choose at least one redirect above

    Click a URL to open it in the redirect checker, or paste the list into the bulk redirect checker. Each should reach its new address in one hop, with a 302. Page and folder URLs appear once your domain is filled in. In a terminal, curl -sIL followed by a URL shows every hop.

  4. Make them permanent (301)

    # Choose at least one redirect above

    Replace the rules from step 2 with these, once step 3 looks right.

Temporary on purpose, for maintenance or a seasonal page? Keep the 302s from step 2 and skip step 4.

Common redirect rules

The redirects people look up most, written by the generator above. The switch at the top shows them for Apache or for Nginx.

All of them on one printable page, for both servers: .htaccess & Nginx redirects cheat sheet (PDF, A4).

Force HTTPS (redirect http to https)

Every http:// address goes to the same address on https://. Behind Cloudflare or another proxy that handles HTTPS, tick the proxy option in the generator: this plain version would redirect forever there.

RewriteEngine On

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

Redirect www to non-www, with HTTPS, in one hop

http://www.example.com goes straight to https://example.com: one redirect instead of two. Apache checks both conditions in one rule; Nginx gives each host its own server block.

RewriteEngine On

# Force HTTPS + canonical host in a single redirect
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

Redirect non-www to www, with HTTPS, in one hop

The same the other way round. Pick one form and use it everywhere: in your links, the sitemap and the canonical tag.

RewriteEngine On

# Force HTTPS + canonical host in a single redirect
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

Remove the trailing slash

/page/ goes to /page, and real folders keep their slash. Not for sites whose addresses end in a slash, like most WordPress sites: WordPress adds it back, and the two redirect each other forever.

RewriteEngine On

# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^ %1 [R=301,L]

Add a trailing slash

/page goes to /page/. Files are left alone, including a robots.txt or sitemap.xml that your CMS generates.

RewriteEngine On

# Add trailing slash (not to files)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.[^/]+$
RewriteRule ^(.+[^/])$ /$1/ [R=301,L]

Redirect one page to another

Catches /old-page and /old-page/, and keeps any ?query. The new address can also be a full URL on another site.

RewriteEngine On

# Single page redirects
RewriteRule ^old-page/?$ /new-page [R=301,NE,L]

Redirect a whole folder

Moves /blog and everything in it: /blog/my-post goes to /news/my-post. To move a folder's pages to the top level of the site, enter / as the new folder.

RewriteEngine On

# Directory redirects (whole subtree)
RewriteRule ^blog(?:/(.*))?$ /news/$1 [R=301,L]

Move a whole domain to a new one

Every address on the old domain, with or without www, goes to the same path on the new one, query string included. It goes on the old site. If both domains share one .htaccess, the generator puts it first.

RewriteEngine On

# Redirect old domain to new domain (keeps path + query)
RewriteCond %{HTTP_HOST} ^(www\.)?old\.example$ [NC]
RewriteRule ^(.*)$ https://new.example%{REQUEST_URI} [R=301,L]

How the rules work

Apache: everything is mod_rewrite, under one RewriteEngine On. Mixing in Redirect lines (mod_alias) for the same URLs leads to double redirects, and Redirect /a /b quietly moves /a/c to /b/c as well, because it matches prefixes. The rules come in a fixed order (domain move, HTTPS and www, pages, folders, trailing slash), so each old address takes one hop.

Nginx: HTTPS, www and the domain move are separate server blocks with a return, the way Nginx recommends. They need a server_name and, on port 443, a certificate that covers those names. Page, folder and trailing-slash rules go inside your site's existing server block. The only ifs are the two safe kinds: the proxy check and the real-folder check.

The traps this handles for you

HTTPS redirect loops behind a CDN
When a proxy such as Cloudflare or a load balancer handles HTTPS, your server only sees plain http, so the usual %{HTTPS} off test is always true and the redirect never ends. Tick "behind a proxy / CDN" and the rule tests %{HTTP:X-Forwarded-Proto} instead. The page, folder and trailing-slash redirects then name https:// themselves, so they don't take a detour through http.
Two-hop chains
Forcing HTTPS and www separately makes http://example.com bounce twice (to https, then to www). With both chosen, they're merged, so every request reaches the right address in one redirect: faster for visitors, and cleaner for search engines.
Trailing-slash loops
Removing the slash leaves real folders alone (they need it, or the server adds it back and the two loop). Adding it leaves files alone, including a robots.txt or sitemap your CMS generates on the fly.
A rule that catches its own target
Move /blog into /blog/archive, or send /about/ to /about, and the rule matches the new address too, so it would redirect it to itself forever. The (?!…) at the start of such a rule leaves the new address alone.
Query strings
Page, folder and domain redirects keep the ?query part of the address.
Everything to the homepage
Send each old address to its closest new page, not to the homepage: Google treats mass redirects to the homepage as soft 404s.

Questions about .htaccess and Nginx redirects

Where do I put redirects in a WordPress .htaccess?

At the top, above the line # BEGIN WordPress. WordPress sends every address it doesn't know to its own index.php, so a redirect below its block never runs: when we tried it, the old URL simply loaded WordPress. Don't put rules between # BEGIN WordPress and # END WordPress either. WordPress rewrites that part, for example when you save the permalink settings, and your rules would be gone.

Why does my .htaccess cause a 500 Internal Server Error?

Apache couldn't read a line, and one unreadable line makes it refuse every request in that folder and below. Put your backup back first (cp .htaccess.bak .htaccess) so the site works again, then look at the server's error log: it names the file and the problem. Usual causes are a typo, a space inside a path, or a directive whose module isn't loaded (RewriteEngine without mod_rewrite) or isn't allowed in .htaccess on that server.

Why is my .htaccess redirect not working?

The usual reasons, most common first: your browser still has an older 301 cached (test in a private window); the rule sits below the WordPress block; the server ignores .htaccess files (AllowOverride None, common on a self-managed Apache); or the site runs on Nginx, which never reads .htaccess, so switch the generator to Nginx. And a rule can't match what it never sees: RewriteRule gets the path without the domain and without the ?query.

How do I fix "too many redirects" after forcing HTTPS?

Usually Cloudflare or another proxy handles HTTPS and talks to your server over plain http. Your server sees http, redirects to https, and the proxy asks again over http, forever. Tick "Site is behind a proxy / CDN" in the generator: the rule then checks the X-Forwarded-Proto header the proxy sends. On Cloudflare, setting SSL/TLS to Full or Full (strict) also ends the loop; that needs a certificate on your server. A trailing-slash rule that fights your CMS loops the same way (see "Remove the trailing slash" above).

Should I use a 301 or a 302 redirect?

A 301 for a move that's for good: Google takes it as a sign to show the new address in search results, which a 302 is not. A 302 for something that comes back, such as a maintenance page or a seasonal offer. Go live with 302s anyway (step 2) and switch once they work, because browsers remember a 301 and a mistake would keep sending visitors to the wrong place. After a site move, Google recommends keeping the redirects for at least a year.

How do I clear a cached 301 redirect?

In your own browser, clear the cached images and files (where you clear your browsing data), or test in a private window, which starts with an empty cache. You can't clear it in your visitors' browsers: they keep following the old redirect until their browser drops it. That's why the steps start with a 302: browsers don't keep one unless the server tells them to.

How do I redirect a URL with a query string?

RewriteRule never sees the part after the ?, so a rule for page.php?id=5 can't match. Test the query in a condition above the rule, and end the target with ? to drop the old query string:

RewriteCond %{QUERY_STRING} ^id=5$
RewriteRule ^page\.php$ /new-page/? [R=301,L]

In Nginx, compare the full address, inside your server block:

if ($request_uri = "/page.php?id=5") { return 301 /new-page/; }

Both redirect only that exact query; page.php?id=6 is left alone.

Related tools

Sources & references