Introduction to Htaccess

Introduction to HtaccessWhat is ‘htaccess’ you may ask yourself. “What the heck, I don’t need that rubbish!” Well, let me tell you one thing, it’s very useful and you’ll be appreciated when you run into htaccess problems. Htaccess has the power to improve you’re website a lot, for an example:

change settings on you’re server, design you’re own custom 404 error pages. Htaccess isn’t that difficult to use, and it’s really simple to learn.

Webspace and support

Some hosts do support. Htaccess but doesn’t publicise it, for security reasons. If you’re on a host that have Unix/Linux installed, or runs any Apache web server it will support htaccess. Another sign, to tell if you’re host supports .htaccess. If you’re having the option to create password protected directories, they’ll need to offer you .htaccess, although most hosts don’t.

Try upload a .htaccess file to you’re host, or try to contact their support center for more information.

Uses of htaccess

You might have heard some of these functions:

Password protecting, redirect users automatically, error pages, change file extensions, block certain IP addresses, or allows certain sessions to view certain pages.

There are many functions, not half of them is mentioned above.

Writing a htaccess file

Writing a htaccess file is easy, but you could be stuck with problems, with the file. Write the appropriated code into a text editor, such as notepad. Saving the file on operating systems like Windows 3.1. Most of the systems, you just need to save it like this:

CODE

“.htaccess”

Include the quotes, for special saving as another file then .txt. If that doesn’t work, name it whatever you want, without the quotes. I used to change my files with a smart ftp program, just a tip off!

404: Error Pages

My first lesson is to teach you how to create a own custom-made error page. This will make you’re page look much more professional and dynamical. To write a custom error page, write this text code:

CODE

ErrorDocument theerrornumber/thefile.php
(You can use whatever you want.)

For a real example, I’ll write my own custom-made error page.

CODE

ErrorDocument 404 /nomatch.php

You can easily configure the path of you’re file, but simply write “ErrorDocument 404/notfound/nomatch.php”.

I’ll write the most common error pages, in the web.

CODE

401 – Authorisation required
500 – Internal Server Error
404 – Wrong page
400 – Bad request
403 – Forbidden

After that, just make you’re own custom-made error page, and upload you’re .htaccess file.

Let's take a look at some functions and commands, that a .htaccess file can be used for.

Useful stuffs

Index file for diffrent directories.
This is pretty useful if you want diffrent index files for diffrent directories. When you're using PHP, this is very useful.
For a example you could write yourock.php as you're directory index file.

CODE

DirectoryIndex index.jsp index.asp index.aspx index.html index.htm

That's just a example, but I suggest you fill them with you're own files. It will show the root directory (if none file was found) in a list.

Stop directory index for showing.
If you're not using a index or main file, all you're directories will be shown in the root directory. This is a security risk, so for preventing this we could write in our .htaccess file:

CODE

Options -Indexes

Okay, now we've done that I should introduce you to some other functions that htacess has to offer (niffy file, ain't it?)
I was thinking about transfer sessions and users, to special files or sites. With this cool thing you could move me to another site or file, even another HTTP host. I'll explain more beneath.

Transfer users

If you've change a file, or just moved you're site to another host and still have the old host active - this is very useful for you to keep you're visitors alert and to keep coming back to you're site. This could be used with META tags also, but this is htaccess so i'll show you how to do it this way :).

CODE

redirect folderfilename.html http://thesite.com

The above is just a example, you could enter another site and filename.
Another function, same as the redirection is that you could move a entire folder of you're choice, if you wanted.
If you choose a directory, it will move all the files that are in that folder - even other directories.

CODE

redirect /directory http://thesite.com/directory/

That's, that. Now we'll head over to authing, and some intresting parts.

Auth a session/user

Okay, this niffy little trick could protect special sites or directories to be listed if you do it the right way. This is a password protection, easy to handle. It may keep users from visiting the files that are password protected.

We'll begin with creating a session & password. We'll require that you create a new htaccess file, 8 characters long file extension. I suggest you open Notepad for this, easy to use. Just write these text in the new password htaccess file:

CODE

username:password

I suggest you encrypt the password with the password generator. There you are adviced to input you're password and username. When you've got the result, paste it into the htaccess password file witht he username / password.

sdsdsd:vJU6uv9VRuH3w
Like that, a example. Save the password file, you can save it wherever you want. It's encrypted, you could put it outside you're root directory for 100% security. So no one can access it from the web. Now we're gonna access the site, and "log in" with our authname and password.

Remember the username (not encrypted) and password (also not encrypted).
http://username:password@www.thesite.com/directory/file


Related Articles

  • Password Protection
    Although there are many uses for a .htaccess file, by far the most popular, and probably most useful, is being able to reliably password protect directories on websites. Although JavaScript can also be used to do this, only .htaccess has total security, because someone must know the password to get ...
  • Prevent Directory Listing
    Whenever you have folders with large amounts of files or important administration files, it is essential to prevent directory listings, otherwise all of the files in the folder will be listed to anyone who wants to see them, including hackers. When hackers can find an administration file, it makes t...
  • No Direct Linking
    If you want to prevent those awful direct linkers, use this tutorial. Open up your notepad (start>>programs>>accessories>>notepad) or another text editor, and copy the code below to it. Name this file .htaccess and make sure it doesnt have an extension on the end, like .txt. If i....
  • Redirection
    Have you changed the navigation structure of your website by moving or deleting pages of your site? If a visitor has the old page bookmarked or comes from a link on another site to it, they will receive a 404 error; page can not be found. Solve this by redirecting the visitor to the new page. The me...
  • Introduction to Htaccess
    What is
  • Change Default Directory Page
    A directorys defauly page is usually index.html, index.htm, or index.php. Therefore, when a visitors goes to www.domain.com they are automatically redirected to www.domain.com/index.html.
    With .htaccess the default directory page can be changed.
    Open your .htaccess file or create a new one. ....
  • Gain More Web Site Control With .htaccess!
    There are many tools of the trade in the world of Web site development, but very few have as many good uses as your .htaccess file. What, you dont have one? Ill admit, information on these little darlings are confusing to some and hard to find...
  • Hotlink Protection
    Bandwidth theft or hotlinking is direct linking to a websites files (images, video, etc.). An example would be displaying a JPEG image you found on someone elses web page so it will appear on your own site, journal, weblog, forum posting, etc.
  • IP Banning
    1. Make a new text file and name it .htaccess or open one you already have.
    2. Find out the persons IP address and substitute it for 000.000.000.000.
    To deny access..
    deny from 000.000.000.000
    To allow access..
    allow from 000.000.000.000
    To ban everyone..
    deny from all
    To add mor....
  • Error Pages
    The standard error pages can be very annoying. When you move a page in your site, visitors have no way of finding where you moved it to in the error page. With custom error pages you can get cool error pages that have the same layout as your site and get rid of the standard ones and replace them wit...

Contact Web Design Outsource and get started today

Need Website Designing, Development, Redesigning, Maintenance and SEO services or help growing your company's web presence? Request a free Quote Now.