301 Redirects in .htaccess: Copy-Paste Examples

By Marko Denic
Contents
  1. Redirect one page
  2. Redirect several pages
  3. Patterns with RedirectMatch and mod_rewrite
  4. Redirect a whole folder
  5. Redirect an entire domain
  6. HTTP to HTTPS
  7. www to non-www (and the reverse)
  8. Add a trailing slash
  9. Query strings
  10. Redirect a URL with a query string
  11. In WordPress
  12. Put redirects above the WordPress block
  13. Or use a plugin
  14. Testing and common mistakes
  15. Test with curl, not the browser
  16. The nginx equivalent
  17. FAQ

A 301 redirect tells browsers and search engines that a page has moved permanently. Google passes the old URL’s ranking to the new one, and visitors with old links or bookmarks land in the right place. (More on 301 vs 302 and the other status codes.)

On Apache servers, which include most shared hosting and WordPress hosts, you add redirects to the .htaccess file in your site’s root folder. The simplest one is a single line:

Redirect one page

The old path is relative to your domain; the new URL should be absolute. This uses mod_alias, which is enabled almost everywhere.

Redirect matches the start of the path, not the exact URL, so this line also sends /old-page/photos/ to /new-page/photos/. It only matches whole path segments, so /old-page without the slash isn’t redirected. To catch both versions, use RedirectMatch 301 ^/old-page/?$ https://example.com/new-page/ instead. Don’t add a second Redirect line for /old-page: it would send /old-page/ to /new-page//.

bash
Redirect 301 /old-page/ https://example.com/new-page/

Redirect several pages

One line per redirect, kept together and commented so they’re easy to maintain. Order matters when one path starts with another: Apache uses the first line that matches, so put the more specific path first (/docs/old/ before /docs/).

bash
# Renamed posts, Oct 2026
Redirect 301 /css-flex/ https://example.com/css-flexbox/
Redirect 301 /grid/ https://example.com/css-grid/
Redirect 301 /contact-us/ https://example.com/contact/

Patterns with RedirectMatch and mod_rewrite

For anything beyond simple path matches, use RedirectMatch (regular expressions) or mod_rewrite (regular expressions plus conditions on the host, protocol and more).

Redirect a whole folder

(.*) captures everything after the folder name, and $1 puts it back in the new URL, so /blog/my-post/ goes to /articles/my-post/.

bash
RedirectMatch 301 ^/blog/(.*)$ https://example.com/articles/$1

Redirect an entire domain

Moving to a new domain? Put this in the old domain’s .htaccess. It keeps the path, so every page lands on its counterpart.

bash
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$ [NC]
RewriteRule ^(.*)$ https://new-domain.com/$1 [R=301,L]

HTTP to HTTPS

bash
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

www to non-www (and the reverse)

Pick one version of your domain and redirect the other to it, so search engines don’t see two copies of every page.

bash
RewriteEngine On

# www → non-www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

# Or: non-www → www
# RewriteCond %{HTTP_HOST} !^www\. [NC]
# RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

Add a trailing slash

Skips real files (-f), so /style.css isn’t turned into /style.css/.

bash
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ /$1/ [R=301,L]

Query strings

Both Redirect and RewriteRule keep the query string by default: /old/?ref=x becomes /new/?ref=x. To drop it, end the target with ? (or use the QSD flag). To match a specific query string, you need RewriteCond %{QUERY_STRING}, because Redirect and RewriteRule patterns only see the path.

Redirect a URL with a query string

bash
# /index.php?page=about → /about/
RewriteEngine On
RewriteCond %{QUERY_STRING} ^page=about$
RewriteRule ^index\.php$ /about/? [R=301,L]

In WordPress

Put redirects above the WordPress block

WordPress manages the section between # BEGIN WordPress and # END WordPress and rewrites it when you save permalinks, so never edit inside it. Add your rules above it: the block ends with a catch-all rule that sends every unknown URL to index.php, so a RewriteRule placed below it never runs.

bash
# Custom redirects
Redirect 301 /old-post/ https://example.com/new-post/

# BEGIN WordPress
# (leave this block alone)
# END WordPress

Or use a plugin

If you’d rather not edit server files, the free Redirection plugin manages 301s from the admin and logs 404s so you can see which old URLs still get traffic. WordPress itself also redirects changed post slugs automatically, using the _wp_old_slug it stores when you rename a post. More useful built-ins in these WordPress tips.

Testing and common mistakes

Test with curl, not the browser

Browsers cache 301s aggressively, so after a mistake you can keep seeing the old redirect even when the file is fixed. curl -I shows the real response every time. Look for 301 and the location header; servers without HTTP/2 show HTTP/1.1 301 Moved Permanently instead.

bash
curl -I https://example.com/old-page/
# HTTP/2 301
# location: https://example.com/new-page/

# Follow the whole chain
curl -IL http://www.example.com/old-page/
  • Redirect chains. http → https → www → new page is three hops. Point old URLs straight at the final destination; chains slow pages down and waste crawl time.
  • Redirect loops. A rule that matches its own target (like redirecting /blog to /blog/ with a pattern that also matches /blog/) causes “too many redirects”. Anchor patterns with ^ and $.
  • Using 302 for permanent moves. R without =301 in a RewriteRule means 302. Always write R=301.
  • Redirecting everything to the home page. Google treats mass redirects to an unrelated page as soft 404s. Redirect to the closest matching page, or let truly gone pages return 404 or 410.
  • A 500 error after saving. A typo in .htaccess takes the whole site down. Keep a copy of the working file before editing.

The nginx equivalent

nginx doesn’t read .htaccess. Redirects go in the server block of the site’s config. Unlike Apache, return drops the query string, so add $is_args$args to keep it. Check the config with nginx -t before reloading.

bash
location = /old-page/ {
  return 301 https://example.com/new-page/$is_args$args;
}

location ~ ^/blog/(.*)$ {
  return 301 https://example.com/articles/$1$is_args$args;
}

FAQ

How long should I keep a 301 redirect?

At least a year, and ideally forever. Google needs time to move rankings, and old backlinks keep sending visitors for years. Removing a redirect turns that traffic into 404s.

Do 301 redirects pass SEO value?

Yes. Google has said 301s (and 302s) pass PageRank. The new page usually takes over the old one’s rankings within a few weeks, as long as the content is similar. More in how to optimize your website for SEO.

Where is the .htaccess file?

In your site’s root folder (the same folder as wp-config.php on WordPress). It’s a hidden file, so enable “show hidden files” in your FTP client or file manager.

Redirect vs RewriteRule: which should I use?

Redirect for simple one-to-one redirects. RewriteRule when you need conditions on the domain, protocol or query string. Don’t mix both for the same URLs, because mod_rewrite and mod_alias run separately.