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

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


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.

  • Jag

    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.

  • Guest

    that’s helpful

    • Guest


  • zahid

    I really a good example of using 301 redirect

  • Anonymous

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

  • Globinch

    This is useful in 404 http error

  • Aaron Nichols

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

  • g1smd

    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$ [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.

  • Shahid-namo

    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.

  • லோகநாதன் நடராசன்

    nice article

  • லோகநாதன் நடராசன்

    gr8 post, thanks

  • Navincshetty

    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:

  • Titogs2000

    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: to having problems what should i do?