What 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








