Help - Search - Members - Calendar
Full Version: How To Use The .htaccess File To Redirect Users
BleepingComputer.com > Bleeping Computer Applications and Guides > Mini guides and how-tos - Simple answers to common questions > Programming and Web Design Mini-Guides
   
nosnhoj#3
How To Use The .htaccess File To Redirect Users



Guide Overview

The purpose of this guide is to teach you how to use the .htaccess file to redirect users to pages that you have modified, deleted, or moved. The .htaccess file is associated with Apache Servers. Not all web hosting services allow modification of this file. Before following this guide, contact your web hosting service provider to confirm that addition and/or modification to this file is allowed.


Instructions
  1. Access your site using the method you normally use to implement changes, and determine whether or not your directory includes a file named .htaccess
  2. If it does, open the file in the text editor of choice. (If so, Skip to step 5)
  3. If it does not, you will need to create one, and upload it to the Root Directory of your site. To do this, open up Notepad or your preferred text editor.
  4. Click File ---> Save as...
    In Notepad, change the Save as type to All Files. Give your file the name .htaccess (the period "." before htaccess is required) and save it to the directory of your choice. Close the file.
  5. Now, open the .htaccess file with the text editor of your choice, and in one complete line, enter the commands for your desired effect, with a space between each command:

    The redirect command has the following format: redirect status /old/old.html http://www.new.com/new.html
    - "Status" is optional and may be one of the following:

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

  6. Save the file, and depending on if it previously existed in the web directory or if you created it, update your site.

    NOTE: If you created the file, it is important that you upload the file to the Root Directory of your site, which is the directory where your default "index" or "home" page resides.


Redirection Notables

1. Be sure not to add "http://www" to the first part of the statement - just put the path from the top level of your site to the page. Also ensure that you leave a single space between these elements:

redirect 301 (the instruction that the page has moved)

/old/old.html (the original folder path and file name)

http://www.new.com/new.html (new path and file name)

2. Each page being redirected needs to occupy it's own line in the .htaccess file.

Conclusion

Be sure to test out your changes after you make them. If something does not work as intended, delete the entry, so it does not create any further problems, and refer to your web host for answers.
Wendy K. Walker
Hi nosnhoj#3,

This seems to be a pretty well written and informative 'howto' that you have here. I am toying around with an Apache web server and this seems to be something that I need to learn more about.

Up until a couple of days ago I didn't have a .htaccess file. Then while perusing an article on securing a server with a password I decided to give it a go and see what that was all about. I followed the instructions that were given to create a password file, htpasswd, and by the time that I had gotten through I had created both a htpasswd, and a .htaccess file.

The .htaccess file looks like this -->

AuthType Basic
AuthName "WARNING!This Is A SECURE AREA, PLEASE LOG IN"

AuthUserFile "c:\Documents and Settings\ADMIN\my_password_file.txt"
AuthGroupFile /dev/null

<Limit GET POST PUT>
require valid-user
</Limit>

That sucker worked too tongue.gif , it made me have to sign in! But that's all that's in it at the moment. Do I just add more lines to make it redirect people to different pages?

OK, now I'm trying to figure out where to put it. According to the way that I'm understanding this -->

NOTE: If you created the file, it is important that you upload the file to the Root Directory of your site, which is the directory where your default "index" or "home" page resides. <--

Then my path to that file should look something like this --> C:\Program Files\Apache Group\Apache2\htdocs\.htaccess <-- right? I mean because that's where the default index thingy is.

Thanks for any insight Boo.

♥ Wendy
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.