Move a page without a redirect and you lose two things at once: the visitors who bookmarked it, and the Google ranking you spent months earning. An htaccess 301 redirect fixes both. It tells browsers and search engines that a URL has moved for good, then hands almost all of its ranking signals to the new address. The catch is the syntax. One stray character in the .htaccess file can take a whole site offline. This guide covers the rules you will actually use, the mistakes that quietly break sites, and a faster way to build them with the free Pixellize htaccess redirect generator.
| What you want to do | Apache .htaccess rule | Type |
|---|---|---|
| Redirect one page | Redirect 301 /old-page /new-page | 301 |
| Redirect a folder | RedirectMatch 301 ^/old-folder/(.*)$ /new-folder/$1 | 301 |
| Move to a new domain | RewriteRule ^(.*)$ https://newdomain.com/$1 [R=301,L] | 301 |
| Force HTTPS | RewriteCond %{HTTPS} off | 301 |
| Remove www | RewriteCond %{HTTP_HOST} ^www\. | 301 |
| Temporary move | Redirect 302 /sale /summer-sale | 302 |
What an htaccess 301 redirect actually does
The number 301 is an HTTP status code. Where a 404 says “this page is gone,” a 301 says “this page moved, here is the new address, update your records.” Browsers follow it instantly. Search engines treat it as a signal to transfer the old URL’s ranking to the new one. Google confirms a 301 is the strongest way to consolidate those signals during a move, in its own redirects documentation.
You reach for a 301 in a handful of situations. Changing a URL structure or a slug. Migrating to a new domain. Switching the whole site from HTTP to HTTPS. Settling on www or non-www so you do not split ranking across two versions. Retiring an old post and pointing readers to a fresher one. In every case the goal is the same. Keep the link, keep the traffic, keep the rank.
301 vs 302 redirects: which one should you use?
Use a 301 redirect for any permanent move, like a new URL structure, an HTTPS switch, or a domain change, because it passes ranking signals to the new URL. Pick a 302 only when the move is temporary, such as a flash sale page or a maintenance swap, since it keeps the original URL indexed.
Here is the thing most people get wrong. They use a 302 for a permanent change because it is the default in some tools, then wonder why the new URL never ranks. A 302 tells Google to keep the old page in its index. If the move is forever, that is the opposite of what you want. When in doubt, and the change is meant to stick, choose 301.
The anatomy of an htaccess redirect rule
Most powerful redirects use RewriteRule, which comes from Apache’s mod_rewrite module. It looks cryptic at first, but it is only four parts: the directive, a pattern that matches the old path, the destination, and a set of flags. Once you can read one rule, you can read all of them.

The [R=301,L] flag does the heavy lifting. R=301 sets the status code. L means “last rule,” so Apache stops processing once it matches. Swap in R=302 and the exact same rule becomes a temporary redirect. That one detail is the difference between keeping your rankings and losing them.
8 htaccess redirect rules you will actually use
Every rule that uses RewriteRule needs RewriteEngine On once at the top of your .htaccess file. Add these blocks below WordPress’s own rules, not inside the # BEGIN WordPress section, or an update will wipe them out.
1. Redirect a single page. The simplest case. Old URL on the left, new URL on the right.
Redirect 301 /old-page.html /new-page.html
2. Redirect a whole folder. Move every URL under /blog/ to /articles/ and keep the rest of the path.
RedirectMatch 301 ^/blog/(.*)$ /articles/$1
3. Redirect to a new domain. Send the entire old site to a new one, preserving every path and query string.
RewriteEngine On
RewriteRule ^(.*)$ https://newdomain.com/$1 [R=301,L]
4. Force HTTPS. Push every visitor to the secure version of your site.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
5. Force www. Make example.com always become www.example.com.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
6. Remove www. The opposite, which is more common today for cleaner URLs.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
7. Redirect by file extension. Point old .php pages to clean .html versions.
RedirectMatch 301 ^/(.*)\.php$ /$1.html
8. Temporary redirect. A seasonal page that will revert later. Note the 302.
Redirect 302 /sale /summer-sale
Redirect vs RewriteRule: when to use each
Apache gives you two ways to redirect, and they come from different modules. Redirect and RedirectMatch live in mod_alias. RewriteRule lives in mod_rewrite. They are not interchangeable, and mixing them in one file is a classic source of redirect loops.
- Use Redirect for plain one-to-one moves. One old path, one new path, no logic. It is easy to read and hard to break.
- Use RewriteRule when you need conditions or patterns: forcing HTTPS, matching www, capturing parts of a URL, or sending a whole domain somewhere with the path intact. The full syntax is in the Apache mod_rewrite documentation.
The rule of thumb: if you can describe the move as “this exact URL goes to that exact URL,” use Redirect. The moment a pattern or a condition enters the picture, switch to RewriteRule. Picking the right tool here is half the battle, and the Pixellize generator chooses it for you based on what you enter.
How do you redirect www to non-www in htaccess?
Redirect www to non-www with two lines in your htaccess file. The first line checks whether the host starts with www, and the second rebuilds the address without it and returns a 301. Place both below the rewrite engine line, then pick either www or non-www and keep that version for SEO.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
The %1 grabs whatever the condition captured, which is your domain without the www. Pick www or non-www once, set it, and never look back. Running both versions live splits your ranking signals across two URLs that Google sees as separate sites.
The same redirects in Nginx
Here is a detail most guides skip. The .htaccess file is an Apache feature. Nginx ignores it completely. If your host runs Nginx, dropping an .htaccess file on the server does nothing, and that confuses a lot of people during a migration. Nginx redirects live in the server block instead.
server {
# Redirect one page
location = /old-page { return 301 /new-page; }
# Redirect a whole domain
server_name olddomain.com;
return 301 https://newdomain.com$request_uri;
}
Not sure which server you are on? Check your hosting panel, or look at the response headers for a Server: line. The Pixellize generator outputs both Apache and Nginx syntax from the same inputs, so you can switch with one click and never translate the rules by hand.
How to edit and test your htaccess file safely
The .htaccess file sits in your site’s root folder, next to wp-config.php. It is hidden, so turn on “show hidden files” in your FTP client or file manager to see it. Before you touch anything, do one thing.
- Back it up. Download a copy first. A bad rule can return a 500 error and take the whole site down, so you want a clean version to restore in seconds.
- Add your rules. Paste them below the WordPress block, save, and upload.
- Test in a private window. Browsers cache 301s aggressively. An incognito tab avoids stale results. Type the old URL and confirm it lands on the new one.
- Check the status code. Use your browser’s network tab or a header checker to confirm it returns 301, not 302 or 200.
After a big move, audit the whole site for redirects that point at dead pages or chain through three hops. Pixellize has a free WordPress site audit walkthrough that covers broken links and redirect chains, and the canonical URL checker guide helps confirm Google sees the version you intend.
Common htaccess redirect mistakes that break sites
Most redirect disasters trace back to the same short list. Keep this nearby the first few times you edit the file.
- Redirect loops. A rule that sends a page to itself, often from forcing HTTPS and www in the wrong order. The browser gives up with a “too many redirects” error.
- Wrong rule order. Apache reads top to bottom and the
Lflag stops processing. Put specific rules above general ones, or the broad rule fires first. - Using 302 for a permanent move. The new URL never inherits the ranking. Did you know a single wrong status code can stall a migration for weeks?
- Missing RewriteEngine On. Without it, every
RewriteRuleis ignored and nothing happens. - Forgetting to back up. A typo returns a 500 error for every visitor. Keep a known-good copy on your desktop.
Think of the .htaccess file like a light switch wired into your whole house. Flip it correctly and everything works. Cross one wire and the lights go out everywhere. That is exactly why generating the rules instead of hand-typing them removes most of the risk.
Skip the syntax with the Pixellize redirect generator
If memorizing flags and conditions is not your idea of a good afternoon, the Pixellize htaccess redirect generator builds the rules for you. Pick your server, enter the old and new paths, choose 301 or 302, and the correct syntax appears in a live preview. No mod_rewrite trivia required.

A few things make it quick. One-click presets handle Force HTTPS, Force www, and Remove www without writing a single condition. You can stack as many redirects as you need in one file. Paste a full URL and it trims to the path automatically. When you are happy, copy the rules or download a ready-to-upload file. Everything runs in your browser, so nothing is uploaded or stored.
It also pairs well with the rest of the Pixellize SEO toolkit. After setting up redirects, the robots.txt generator and the SERP snippet preview help you tidy up the rest of your technical SEO in the same browser tab.
Wrapping up
An htaccess 301 redirect is one of the highest-impact SEO moves you can make, and it takes about a minute once you know the syntax. Permanent move, use 301. Temporary one, use 302. Back up the file, test in a private window, and confirm the status code before you walk away. Do that and a URL change costs you nothing in rankings or traffic.
When you would rather not wrestle with mod_rewrite, let the Pixellize htaccess redirect generator write the rules for you, for both Apache and Nginx, free and with no signup. Build your redirects, copy them in, and get back to the work that actually moved the page in the first place.