.htaccess redirects

Generate Apache redirect, HTTPS and www rules.

Add the generated code to the .htaccess in your site root. RewriteEngine only needs to be turned on once.

AD

You renamed a page, moved a blog to a new domain, or finally installed an SSL certificate — and now half your traffic lands on URLs that no longer exist. On Apache the fix usually lives in a small file called .htaccess, but the syntax is easy to get subtly wrong.

This generator writes the four rules people need most often, using the exact directives Apache expects. Pick a type, fill in the paths if the option needs them, and copy the result.

How it works

What each option produces

OptionRule it writes
Specific path → URL (301)Redirect 301 /old-page https://example.com/new-page
Specific path → URL (302 temporary)Same line with 302 instead
Force HTTP → HTTPSRewriteEngine On, then RewriteCond %{HTTPS} off and RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
non-www → wwwA RewriteCond matching hosts that do not start with www., redirecting to https://www.%1/$1
www → non-wwwThe mirror image: hosts starting with www. are sent to the bare domain

The two path options use mod_alias, which needs nothing switched on. The other three use mod_rewrite, so the output starts with RewriteEngine On.

Where the code goes

Paste it into the .htaccess file in your site root — the same folder as your index page — creating the file if it is not there. The leading dot matters, and some FTP clients hide such files by default.

If you already have rewrite rules, keep only one RewriteEngine On in the file and place it above every RewriteRule. Order matters: Apache runs the rules top to bottom, so put site-wide redirects such as HTTPS before application routing rules.

Before you trust it

Test with curl -I https://yourdomain.com/old-page and look at the Location header and status code rather than clicking in a browser, because browsers cache 301s aggressively and will keep following an old rule after you fix it. Test in a private window if you must use a browser.

One caution: a 301 is cached by clients and treated as permanent by search engines, so use 302 while you are still experimenting.

Terms explained

301 vs 302
A 301 tells browsers and search engines the move is permanent and passes ranking signals to the new URL. A 302 says it is temporary and leaves the original indexed. Use 301 unless you plan to restore the old address.
RewriteEngine On
Switches on Apache's mod_rewrite for that directory. It only needs to appear once per file, and it must come before any RewriteRule.
RewriteCond
A condition that must be true before the RewriteRule directly below it runs. `%{HTTPS}` and `%{HTTP_HOST}` are server variables it tests against.
[R=301,L]
Flags on a RewriteRule. `R=301` makes it an external redirect with that status; `L` stops rule processing so later rules do not fire on the same request.
Old path
The path portion of the URL only, starting with a slash — `/old-page`, not the full `https://...` address.
%1 and $1
Backreferences. `%1` is the group captured by the last RewriteCond (the domain), `$1` is the group captured by the RewriteRule pattern (the requested path).

Frequently asked questions

My rules do nothing at all. What should I check first?

Usually the file is being ignored. Apache only reads `.htaccess` when `AllowOverride` is set to `All` or at least `FileInfo` for that directory in the server config, and mod_rewrite has to be enabled. Also confirm you are actually on Apache — Nginx, Caddy and IIS never read `.htaccess`.

Does the HTTPS rule work behind Cloudflare or a load balancer?

Often not. When TLS terminates at a proxy, Apache receives a plain HTTP request, so `%{HTTPS} off` stays true and the rule redirects forever in a loop. In that setup test the forwarded header instead, with a condition on `%{HTTP:X-Forwarded-Proto}` not being `https`.

Will a 301 hurt my search rankings?

A single, correctly targeted 301 is the standard way to move a page and passes most ranking signals through. The damage usually comes from redirect chains (A→B→C) and from sending every old URL to the home page instead of to the closest matching page.

Can the path option redirect a whole folder?

Yes, and it does so whether you meant it to or not. `Redirect` matches by prefix, so `/blog` also catches `/blog/post-1` and appends the remainder to the target URL. For an exact single-URL match use `RedirectMatch 301 ^/blog$` with your target instead.

Can I combine the HTTPS rule and the www rule?

You can, but keep them inside one `RewriteEngine On` block and order them so a visitor never takes two hops. Ideally `http://example.com` reaches `https://www.example.com` in one redirect rather than passing through `https://example.com` on the way.

AD