Redirect webpages or URLs using .htaccess

   Posted 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.

13 Responses to “Redirect webpages or URLs using .htaccess”

  1. November 04, 2008 at 11:17 pm, Jag said:

    How long does it take to actually redirect? I followed exactly as stated and it doesn’t redirect…Could this be that I am on a free hosting account? It supports uploading .htaccess files.

    Reply

  2. January 22, 2010 at 12:58 am, Guest said:

    that’s helpful

    Reply

    • January 22, 2010 at 1:20 am, Guest said:

      true

      Reply

  3. February 03, 2010 at 5:46 am, zahid said:

    I really a good example of using 301 redirect

    http://www.watchlivetv.us

    Reply

  4. February 11, 2010 at 5:47 pm, Anonymous said:

    Thanks for posting, I always seam to forget the syntax.

    Reply

  5. March 31, 2010 at 9:43 am, Globinch said:

    This is useful in 404 http error

    Reply

  6. March 31, 2010 at 2:04 pm, Aaron Nichols said:

    how do you write a redirect from http://website.com to http://www.website.com both seem to work for me and I do not want search engines to think i have mirror sites.

    Reply

  7. May 03, 2010 at 6:34 am, g1smd said:

    If you have any rules using RewriteRule within your .htaccess file, do NOT mix Redirect or RedirectMatch directives with the configuration of the same site. You’ll end up with problems because RewriteRule comes from mod_Rewrite and the other two directives come from mod_Alias. You cannot guarantee the processing order. If redirects are processed after rewrites, you’ll expose internal filepaths back onto the web as URLs.

    RewriteRule ^oldpath.ext$ http://www.example.com/newpath.ext [R=301,L]

    RewriteRule allows both external redirects (when a domain name and/or R flag is present) and internal rewrites (when the target is just an internal filepath). All rules always need the [L] flag.

    RewriteRule can only see the path part of the literal GET request. With a preceding RewriteCond, you can also test the value of HTTP_HOST, QUERY_STRING, SERVER_PORT, REQUEST_URI, THE_REQUEST, REDIRECT_STATUS, and other request variables.

    Reply

  8. September 01, 2010 at 11:03 pm, Shahid-namo said:

    How long does it take to actually redirect? I followed exactly as stated and it doesn’t redirect…Could this be that I am on a free hosting account? It supports uploading .htaccess files.

    Reply

  9. December 29, 2010 at 6:53 am, லோகநாதன் நடராசன் said:

    nice article

    Reply

  10. December 29, 2010 at 6:53 am, லோகநாதன் நடராசன் said:

    gr8 post, thanks

    Reply

  11. January 10, 2011 at 8:19 pm, Navincshetty said:

    You can use .htaccess file in different folders too like:
    /.htaccess – Main or Root folder
    /content/.htaccess – .inside content folder
    /content/html/images/.htaccess – inside images folder…try userself and protect your folders.

    More Info: http://node.technohub.in

    Reply

  12. May 10, 2011 at 3:15 pm, Titogs2000 said:

    How i can redirect an old web to a new web with their parameters?The command line im using for the .htaccess is redirect 301:http://200.37.59.94/juntosformajosdesarrollo.asp?por=1&en=18 to http://www.acerosarequipa.com/index.php?id=juntosforjamosdesarrollo0Im having problems what should i do?

    Reply

Leave a Reply