Pages

Subscribe

How To Use .htaccess 301 Redirect

Using .htaccess to redirect your visitors from one page to another is the cleanest fastest way of making a redirect. This is the same method I use to redirect readers of this blog from DavePit.com to DavidPitlyuk.com. I do this because DavidPitlyuk.com may be too hard to spell, so I added DavePit.com which is easy, and added a simple re-direct to move the readers over to the correct domain.

You can use .htaccess to redirect entire domains, folders, specific files, and more. John Chow uses an .htaccess redirect to set his preferred domain, something that can better your site from an SEO standpoint. I’ll go over how to do this as well.

Things To Note

Keep in mind that .htaccess is only supported on non-Windows servers, so any of you guys using Windows need to utilize another method to redirect your users. Also, if you are creating the .htaccess file in Windows, you’ll find that it doesn’t like filenames with no name on it. The workaround for this is to make the file something like htaccess.txt, and after uploading the file, rename on the server. Make sure to change http://www.example.com to your own domain name in any of the examples.

So let’s go over a few common methods of using the redirect:

Redirect An Entire Domain

This is what I use to redirect everybody who accesses this site at DavePit.com and transfer them to DavidPitlyuk.com.

Redirect 301 / http://www.example.com/

Redirect non-WWW to WWW (example.com > www.example.com)

This is the setting the preferred domain that I was talking about earlier. It will redirect all of your traffic at the example.com level and transfer it to www.example.com

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

Redirect A Single File Or Folder

This will be used if you want to redirect something like http://www.example.com/blog2 to http://www.example.com/blog or http://www.example.com/blog.php to http://www.example.com/blog.

Redirect 301 /OldPageOrDirectory.html http://www.example.com/NewPageOrDirectory.html

There are many other methods of using .htaccess to redirect, but in the most cases this will cover what you need.

0 comments: