An .htaccess file is a very simple text document. This file states in the root directory or in a subdirectory and it can makes changes to the server. This server needs to be Apache. Before you can use .htaccess files you need to be sure that your host supports it. If your host supports it you can make .htaccess files.
What can you do with .htaccess?
Well, you can reach all sorts of things. Like:
Make own error pages.
Protect directories with .htaccess and .htpasswd.
Choose for www for you domain or not.
Make a redirect.
Block IP-addresses.
Set your index page.
Avoid directory browsing.
A HTML file parse as PHP file.
Avoid hot linking
Mod-Rewrite
If you want to know more about Mod-Rewrite and choosing for www for your domain name and avoid hot linking. You can read this article, made by Stefan van Elsas www.tbforum.com/artikel/72269.html
You can make .htaccess files with your text-editor. Just type in the text and save it. With no name, it’s just an extension. If you can’t save it like that, save it as .htaccess.txt and upload it to your server. You can rename it to .htaccess.
Own error pages
You probably have seen this before on an internet site: ERROR 404 – FILE NOT FOUND. You’ll get this error if you go to an internet page that doesn’t exist. Well, if you are a webmaster and if you don’t want to let see this message to your visitors. You can make your own error pages or you can send your visitors to another page, like your index. With this example you sent your visitor to the file: error/404.shtml if a file doesn’t exist.
ErrorDocument 404 /error/404.html
Here’s a list of all error-codes. You can make of the most a special document.
400 – Bad Request
401 – Authorization Required
402 – Payment Required
403 – Forbidden
404 – File Not Found
405 – Method Not Allowed
406 – Not Acceptable
407 – Proxy Authentication Required
408 – Request Time-out
409 – Conflict
410 – Gone
411 – Length Required
412 – Precondition Failed
413 – Request Entity Too large
414 – Request-URI Too Large
415 – Unsupported Media Type
500 – Internal Server Error
501 – Method Not Implemented
502 – Bad Gateway
503 – Service Temporarily Unavailable
504 – Gateway Time-out
505 – HTTP Version Not Supported
Protect directories
If you want a protected directory. You also can use .htaccess. First you need to make a directory on your server. The next thing to do is making the .htaccess file. If you have DirectAdmin or software like that on your server. You don’t need to make the .htaccess file. With DirectAdmin you can make protected directories without typing any code, except for a username and a password. But if you want to make the file. This is what you need:
For a protected directory, you also need an .htpasswd file. And in the first rule of the .htaccess file is the pad to this .htpasswd file. In your .htpasswd file state you username and password separated by a “:”. So something likes this:
username:password
You need to place the .htaccess file in your protected directory and you place your .htpasswd file anywhere you want. But the pad in .htaccess needs to be good. If it’s wrong, you can’t login. This is how it looks in your browser:
Make a redirect
You can send your visitor to another page or directory with .htaccess. You can do that because a page is somewhere else or things like that. To do that, you need this code:
Block IP-Addresses
You can block IP-Addresses with .htaccess. Or you can block all addresses and accept your own. This code shows how you allow everyone and you block two addresses.
order allow, deny
deny from 123.123.123.123
deny from 234.234.234.234
allow from all
And this code shows how you block all IP addresses and you allow 2 other.
oder allow, deny
deny from all
allow from 123.123.123.123
allow from 234.234.234.234
HTML file as PHP file
You can parse an HTML file as a PHP file. Now you will think: “why would you do that?” Well if you have an old website with only HTML files and you are linked by other websites or you are in Google with your HTML files and you will change to PHP files. Then all the HTML files will be gone and that is bad for Google and your link partners. Well, you can do this also with a redirect, but there’s a better manor to do that. You can parse a HTML file as a PHP file. And you can do that with this code:
AddType application/x-httpd-php .php .htm. html
This code will say to Apache that all the .htm and .html files need to be parsing as .php. You can test it yourself. Make a HTML page with a php code inside. Add this code to you .htaccess file and look!
Set your standard page in your directory
Not everyone wants an index.php or index.html as standard page. With .htaccess you can change this. If you want hous.html as standard page. You can use this code:
DirectoryIndex hous.html
You also can use more files. By this code the server first will try to go to hous.html, if that file don’t exists, he will try garage.html.
DirectoryIndex hous.html garage.html
Avoid directory browsing
Directory browsing is that if you don’t have an index page, you can see a list of all files. By some servers is this already standard that nobody can see that list. And the visitor get’s a forbidden message. But if it doesn’t is standard. You can use this simple code.