How to set up a 301 redirect using htaccess

Published on July 20, 2021

How to set up a 301 redirect using htaccess

How to set up a 301 redirect using htaccess you ask? Better yet, why would one do such a thing?

Both are questions I was asking myself late last week. A friend had mentioned that there were SEO implications to having both and floating on the net.

See, Google et al treat and as separate sites. Technically, www is a subdomain, similar to, where is the domain, and this is the subdomain.

In other words, and might be stealing page rank and search juice from each other. This will not do!

This is where the 301 redirect comes in. A redirect simply tells any browser that’s looking for to automatically go to Setting up is pretty simple, if you’re comfortable editing files on a server.

Assuming you know your way around an ftp program (I use Transmit, for the mac), point your self at your hosting account. Look in the root or public_html folders (setup may be different, according to your host) and try to find a file called .htaccess Note the . before the name. This means that it’s a hidden file. You may have to look for “Show Hidden Files” in your ftp program. Try looking in Preferences or under the View menu.

Not all blog programs require .htaccess, and not all hosting plans allow it’s use. So you may be out of luck using this technique. Not to fear though! lists 7 different ways to create a 301 redirect.

Assuming you’ve found the .htaccess file, make a copy of it somewhere for backup. Next, open the file in a text editor. The following is from (reposted here with permission).

Redirect to www (htaccess redirect)

Create a .htaccess file with the below code, it will ensure that all requests coming in to will get redirected to

The .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)

Options +FollowSymlinks

RewriteEngine on

rewritecond %{http_host} ^ [nc]

rewriterule ^(.*)$$1 [r=301,nc]

Please REPLACE and with your actual domain name.

Note* This .htaccess method of redirection works ONLY on Linux servers having the Apache Mod-Rewrite moduled enabled.

You can stick this at the end of your existing .htaccess file. Now replace the version on the server with your modified file (you did make a backup of the original file, right?). Test it out.

You can use a 301 redirect for a variety of things, including moving domains (just point your old URL to the new site), directing search engines when you move a file, and more. Creating a 301 redirect is a gentle way to enter the dark world of .htaccess. With a little knowledge, and a good command of copy and paste, you can customize your .htaccess file to do some very useful things. For a great list of stupid htaccess tricks, check out Perishable Press.

Keep reading

More posts from our blog