Redirect webpages or URLs using .htaccess

   Posted September 30, 2008 by David Kirk in HTTP

The .htaccess file is a powerful tool that most casual users have not yet discovered. Here I’ll show how to use it to redirect users from one site to another. For this example, we will redirect users from /techrx/test to http://tech-recipes.com


This recipe requires you to be able to create, edit, and set permissions of files. In unix-like systems, this can be done many ways (pico then chmod) for example. Other servers will have web-based mechanisms for these purposes. Finally, some ftp programs also have this ability.

1. In the directory you want to redirect, create (or edit if it already exists) the .htaccess file. Note the leading dot which is required.

2. In one complete line, without any additional line feeds, enter the direct command.

The redirect command has the following format: redirect status old-url new-url
– Status is optional and may be one of the following:

  • permanent – (301) resource has moved permanently.
  • temp – (302). resource has moved temporarily.
  • seeother – (303) resource has been replaced.
  • gone – (410) resource has been permanently removed. (When this status is used the new-url argument should not be used.)

3. Save the file

4. If you created the file, you may need to set the permissions to be web readable. If you have access to the chmod command then type chmod 664 .htaccess.

So in my example, my .htaccess file would contain the line
redirect permanent /techrx/test http://tech-recipes.com/

 

About David Kirk

David Kirk is one of the original founders of tech-recipes and is currently serving as editor-in-chief. Not only has he been crafting tutorials for over ten years, but in his other life he also enjoys taking care of critically ill patients as an ICU physician.
View more articles by David Kirk

The Conversation

Follow the reactions below and share your own thoughts.