headerIMG

B2B Articles - April 26, 2010

How to use and create .htaccess files

website security - tools, tips and techniquesYou can make configuration changes on a per-directory basis by creating .htaccess files on an Apache server (although other server options do exist). The directives that you create within the .htaccess file will control the directory the file is placed within--place the file at the root level of the directory.

Guide on how to use and create htaccess files

How to create an .htaccess file:
.htaccess fileLike most any file type, you can create a file by renaming a simple text document and giving it the name ".access". However, Windows users may need to make changes to be able to view .htaccess files within their system, which is a simple configuration change to allow viewing of "hidden files." Dreamweaver may not recognize .htaccess files due to the fact that they are an unrecognized file extension by default. If you use Dreamweaver or a similar program to edit code, select to "open the file using Dreamweaver." Two programs that are great for editing .htaccess files are: NotePad or SimpleText. By default, Notepad it will save the file .htaccess.txt--so you will need to remove the .txt extension.

Note: .htaccess is the file extension. It is not file.htaccess or somepage.htaccess, it is just named .htaccess

How to Select the Right Web Design Agency [Free eBook PDF]

General notes on .htaccess

  • htaccess files must be uploaded as ASCII mode, not BINARY
  • You will need to CHMOD the htaccess file to 644 or (RW-R--R--) so that the file is usable by the server (and prevents it from being read by a browser)
  • Commands in htaccess are mostly single line only: you may need to turn off auto word wrap
  • .htaccess will also affect sub directories in which they are placed

.htaccess Sample Scripts

Use .htaccess to password protect a directory
You can use .htaccess to password a directory on your server. There are numerous approaches to creating an authentication system. Htaccess can be used for passwords. Place the username and password in the htpasswd file.

AuthUserFile /full/path/to/.htpasswd
AuthType Basic
AuthName "My Directory To Be Protected"
Require valid-user

The full/path/to/.htpasswd should be the full path to the .htpasswd file. On Windows for instance, it could look like this: C:\wwwrootlevel\username\.htpasswd.

The above .htaccess file will password protect all files in the folder that it is placed in, including all sub-folders as well.

Use .htaccess to protect a single file

AuthUserFile /full/path/to/.htpasswd
AuthType Basic
AuthName "My Secret Page"

<Files "mywebfile.html">
Require valid-user
</Files>

This script will password-protect just the mywebfile.html  file in the folder where you put the .htaccess  file.

Change your default directory page

You can specify a certain file to be your index file (directory index). By default, index.html or index.htm is often the default index for directories. Let's say you wanted to use a different file as your directory index, you could by using .htaccess to specify the file.

DirectoryIndex filename.html

Redirects using .htaccess

You can create redirects using .htaccess as well. For example, 301 permanent redirects are very important for SEO purposes since they let Google and other search engines know if a file has been moved and where to find this file.

Redirect /olddirectory/oldfile.html https://yoursite.com/newdirectory/newfile.html

Tel 212-993-7809  

Ironpaper ®
10 East 33rd Street 
6th Floor
New York, NY 10016
Map

Ironpaper - B2B Agency

B2B Marketing and Growth Agency.

Grow your B2B business boldly. Ironpaper is a B2B marketing agency. We build growth engines for marketing and sales success. We power demand generation campaigns, ABM programs, create B2B content, strengthen sales enablement, generate qualified leads, and improve B2B marketing efforts.

Ironpaper Twitter Ironpaper Linkedin