Status
Not open for further replies.

timtamboy63

Active Member
885
2009
0
0
What is .htaccess?

.Htaccess is an incredible useful file that you can only use when using Apache(to my knowledge). It's got a tonne of functionality, which I will be detailing below. First of all, I should mention that most FTP Clients won't show you a .htaccess file because most of them are configured to hide "hidden" files, which are files with a "." in front of them. Luckily this can be changed to show all files.

Htaccess files allow you to specify commands(kinda) which the server follows. You can of course put these commands in your httpd.conf file but most people aren't hosted on Dedicated Servers or VPS's and don't have access to it.


Common Functions
Error Handling
Your .htaccess file can control what happens when the user lands on a 404 or 403, etc etc page. This is how most sites use custom error pages.
The command is ->
Code:
ErrorDocument ERRORCODE PATHTOFILE
So say for example you had a custom 404 error page located in /404.html, your command would be:
Code:
ErrorDocument 404 404.html

Controling Access
This is also mighty useful for securing your site. You can control who(or more specifically which IP Addresses) can access certain parts of your site.
Okay lets say you had a few files uploaded for emergency, stored in /emergency. These files had the potential to reak havok on your site, but you had them there just incase something messed up. You didn't want anyone to access it unless something screwed up and you had to go and manually access it, this would save you reuploading the file.

What you would do in this case is put a .htaccess file in /emergency which contains:
Code:
order allow,deny
deny from all
This way, no-one could access it unless you deleted the htaccess, which you presumably would do in an emergency.

But what if you wanted a certain IP Address to access that section?
Luckily, htaccess files can do that as well.
Heres what you would put in the htaccess file instead.
Code:
order deny,allow
deny from all
allow from YOURIPHERE

Htaccess files can also protect only certain files from being viewed.
To do this, wrap your corresponding commands in <Files FILENAME>
For example to protect Database.zip files from being viewed, you would add this to your main .htaccess
Code:
<Files Database.zip>
order deny,allow
deny from all
allow from YOURIPHERE
</Files>

To protect ALL .zip files, the code must be slightly modified. The new code would look like:
Code:
<Files ~ "\.(zip)$">
order deny,allow
deny from all
allow from YOURIPHERE
</Files>

Htaccess can also do a lot more for security and I encourage you to do some research :)

Redirects
Htaccess files can also be used for redirecting.
The command for this is:
Code:
redirect TARGETFILE REDIRECTEDTO
For example to redirect /google.html to www.google.com, the command would be

Code:
redirect /google.html http://www.google.com

A few more tricks
The Ifmodule tag is useful, it allows you to check whether the server has a certain module enabled.
For example,
Code:
<IfModule mod_autoindex.c>
ADDITIONAL CODE HERE
</ifModule>
That code would check whether the module mode_autoindex.c is enabled, and if it is, it would execute whatever is inbetween those lines.

You can also change the first page the server looks for with htaccess, usually index.html.
To do this add the following to your htacecss
Code:
DirectoryIndex FILE1 FILE2 FILE3
You can specify as many files as you want, and the server will look for them in order.
For example, with the code
Code:
DirectoryIndex blah.php blahblah.php blahblahblah.php
The server would look for blah.php first, and if that doesn't exist it would look for blahblah.php, etc

Thats about it for this basic tutorial, I've only scratched the surface of the mighty .htaccess file and yet my fingers are dying :)
I seriously encourage you to do some research into it, it is mega useful :)

I might continue this later, but for now that's it.

Oh and this was written by timtamboy63 for chrixel.com and wjunction.com
 
12 comments
Nice work. Truely appreciate ur work.
.htaccess can do wonders sometime, u just need to know how to use it.

If u use it wrong ur WEB Dir would be unavailable to u via web.
 
I'll be posting more on chrixel btw, but i'll keep posting a few on here
Oh and I just fixed a small error, its httpd.conf not http.conf
 
noob question. i wanna rewrite mydomain.com/?c=signup displaying mydomain/signup.php, how's that using htaccess? sorry for my english.
 
Status
Not open for further replies.
Back
Top