How to redirect pages with .htaccess

Dan Counsell · Founder of Realmac SoftwareUpdated

We’ve made RapidWeaver, a website builder for Mac, since 2004. Each guide works with any tool, then shows how in RapidWeaver Elements.

When a page moves, a redirect sends visitors and search engines from the old address to the new one. Without it, old links and search results lead to a “page not found” error.

What is an .htaccess file?

.htaccess is a settings file for Apache, the web server many hosts use. It sits in your site’s folder and can set redirects, force HTTPS, add security headers and more. The dot at the start makes it hidden on a Mac, so press Command-Shift-full stop in Finder to see it.

If your host uses Nginx or another server, .htaccess won’t work. Ask your host how they handle redirects.

301 or 302?

  • 301 means the page has moved for good. Search engines move the old page’s ranking to the new one. Use this almost always.
  • 302 means the move is temporary. Use it for a short-lived change you’ll undo.

.htaccess redirect examples

Redirect one page:

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

Redirect a whole folder, keeping the rest of the address:

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

Redirect the whole site to a new domain:

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

Force HTTPS:

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

Send www to the plain domain:

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

Tips that save headaches

  • Put specific rules above general ones. The server uses the first rule that matches.
  • Redirect in one hop. If A moved to B and then B moved to C, point A straight at C.
  • Don’t redirect everything to the home page. Send each old page to its closest new one.
  • Test in a private window, because browsers remember 301s.
  • Keep a copy of the file before you change it. One typo can take the whole site down with a “500" error; put the copy back to fix it.

Add an .htaccess file in RapidWeaver Elements

  1. Right-click in the page list and choose New File.
  2. Name it .htaccess and keep it at the top level of your site.
  3. Add your rules in the code editor, then publish.

The Elements manual has a recommended starting .htaccess with security headers and compression.

Questions about .htaccess redirects

Why isn’t my .htaccess redirect working?

Check that your host uses Apache, that the file is named exactly .htaccess and sits in the right folder, and that your browser isn’t showing a remembered redirect. A rule higher up may also be matching first.

How long should I keep redirects?

At least a year, and longer for pages with lots of links. There’s little harm in keeping them for good.

Can I redirect without .htaccess?

Some hosts have a redirects page in their control panel. A page with a meta refresh tag also works, but search engines treat it less well than a 301.

Get RapidWeaver Elements for Mac

Design, build and publish your site without code, then host it anywhere. Try it free on your own project.

Download

Confirm your email and we'll send your code. Unsubscribe any time. Privacy policy