How to do a .htaccess 301 redirect

This article is a part of the redirect series I am creating. To find the full series read this article and find a link to the full resource at the bottom.

Duplicating your content to the new location or new domain name

When you’re about to redirect your content to a new server or to a new domain you should not mix it up with changing your file structure at the same time. This is very important. If you change your domain name and your catalog structure at the same time you will have a whole mess to cover, especially if you are moving servers at the same time.

You should redirect one step at the time and carefully disect whether or not additional changes have to be made. Perhaps you don’t need to do something, but it would look neat. Please keep those changes to the very minimum and think of functionality, SEO value etc. before making your changes.

It is all dependent upon situation, but if you don’t want to loose too much in rankings you should copy all the files, the file structure etc from the old place and upload it on a 1 to 1 basis to the new place. This means that you should put:

  • http://someolddomain.com/someoldcatalog/someresource.html

to

  • http://somenewdomain.com/someoldcatalog/someresource.html

Don’t change the catalogs at this time. If you desperately have to, then sure. One instance where it actually makes sense is when your catalogs don’t make any sense in the url. Otherwise, drag all the files from your old server to your desktop, then upload them to the new server.

For those of you running databases or dynamic pages, you should export your data base and upload it to the new one (if not the same one) and change the data base connection configuration on the new installation of your web platform so that they fetch the same content in the same way as on the old platform.

Creating a .htaccess file

The .htaccess file is a file that basically tells your visitor server/computer/user the seccurity, redirect, url etc structures of your server/files/resources etc. It is the first document that the visiting client fetch when they arrive at your website. If you log onto your webserver via ftp or something similar you might find that there is no .htaccess file there.

If this is the case then you should go ahead and create one. Do it in Notepad or some similar code editor so that no crap or extension is added to your file. Ie. it should not be .htaccess.txt or .html or .xml or any other extension. It should simply be .htaccess. Save it as .htacces – don’t forget the dot. If you already have a file on your server then download that one to your computer to edit it.

1. Redirection of Static pages

  1. Place this code in your .htaccess file:
    redirect 301 /old/old.htm http://www.newdomain.com/somefile.html
  2. If the .htaccess file already contains content, then scroll to the end and make sure you leave one line between the old code and the new code
  3. Save the file (still no extension)
  4. Upload it to the root file of your server
  5. Go to a standard web browser
  6. Type the old url
  7. If you’ve done this correctly you should end up in the new location

Usually, the main problem is that you haven’t uploaded the .htaccess file to your root folder or your .htaccess file has an extension or you have forgotten the dot, the ., the [.] the ………….. THE DOT!! Don’t forget the dot in front of the htaccess.

Locating your root folder

You should always put your .htaccess file in your local folder. The reason for this being that it is where your server looks for it when another server asks for it. The root folder is usually the one you see where you only have a / in your browser window address field. This might vary however. Some web hotels have a www_root folder in which you find your files, or they have a htroot, public_html/ or some other root file. My suggestion is that you either ask your hosing provider or test/fail/succeed.

I find my root file here through my ftp.

2. Redirection of Dynamic pages or Entire domains

When redirecting pages that are fetched from a database you should do this in a different way. Basically use the following code:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=[page-id-nr]$
RewriteRule ^/page.php$ http://www.example.com/newname.htm? [L,R=301]

You will have to replace [page-id-nr] with the number of the page you are redirecting. Ie. if you have some page with the url http://www.example.com/page.php?id=256 such as the normal id’s for a wp-blog looks if you haven’t done anything with the permlink structure. Then the code above should look like this:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=256$
RewriteRule ^/page.php$ http://www.example.com/newname.htm? [L,R=301]

I know. Now you’re thinking –> OMG WTF Do I have to do this for all my pages. And my reply is YES IF you are changing everything at once. BUT if you’re keeping the same file structure but only changing domain or server then you can skip the above and simply put the code:

redirect 301 / http://www.you.com/

If the above doesn’t work for you, then check if your .htaccess file has the proper name, the proper extension (meaning NO EXTENSION), and that it includes the ., the dot, the [.] (can’t stress that enough).

Then try this alternative:

redirectMatch 301 ^(.*)$ http://www.domain.com
redirectMatch permanent ^(.*)$ http://www.domain.com

If this doesn’t work then you should e-mail me and I should update the post.

What to think of when changing server or/and domain name

If you are like me and you have an obsessive compulsive disorder against duplicate content. Then you should do the following whilst you are working on your redirection process. Let’s push back a couple of steps and take it at a macro level from the top.

  1. Block the new domain in your robots.txt file (you can do this from your webmastertools central)
  2. Upload the old content to the new place
  3. Make sure your old content is visible on the new place by typing in the new url or by requesting the new server
  4. Make the redirection
  5. Remove the robots.txt block from your new domain

This is important, but not the most important thing to do. If you want to make sure Google doesn’t find the content on the new domain then you should noindex it until the redirection is in place. There are some “creative” reasons to do this as well, but I’ll tell you more about that in another article.

There is a module called mod_rewrite on Apache servers. In another article I will cover how you can use your mod_rewrite to do this very easily and powerful for yourself and your redirection process.

This article is a part of a series of articles covering redirections. Once the full tutorial is done you can find it under the Search Engine Optimization tab.

Possibly related posts:

13 thoughts on “How to do a .htaccess 301 redirect”

  1. i need to redirect 301
    /?blabla=whatever dynamic no 4 digit
    to
    /bla/bla/bla/bla/same dynamic no what was in old url/

    this seems to be work

    RewriteCond %{QUERY_STRING} ^blabla=([0-9][0-9][0-9][0-9])$
    RewriteRule ^index.php$ http: //www.blablabla.com//bla/bla/bla/bla/?/ [R=301,L]

    the redirected url is http: //www.blablabla.com/bla/bla/bla/bla/?blabla=whatever dynamic no 4 digit

    how to cut away this ?blabla= from there and add to the end of new url /
    Anybody have idea?

    Reply
  2. I am moving the domain something like old.com to new.net
    – I need to redirect every pages on old.com to old.net however, I don’t have the permission to access old.com. to put the redirection. Is it possible to create the htaccess and put in new.net’s root directory?

    I would appreciate if you could guide me through these

    Reply
  3. Thank you for a clear and straight forward answer. I got very close to what I needed from various other forum posts with no success. Your blog laid it out nicely – considering it IS a very simple solution. I was missing the “RewriteEngine on” DOH!

    Reply
  4. Thank you for the wonderful Tutorials you make available. I’ll be coming back to look at the Fecebook Marketing series.

    We’re considering changing some 200+ pages on our site from .shtml extensions to .php- on the some domain

    Can we use a single instruction in the .htaccess file to achieve a ‘global’ 301 redirect?

    And, Yes, I realise we’ll have to do other things on the pages to effect the change!

    Many thanks,

    John

    Reply

Leave a Comment