Play with .htaccess files

Aug 30, 2007 14:14 GMT  ·  By

.htaccess (Hypertext Access) is the default name of Apache's directory-level configuration file. .htaccess is used to provide security restrictions for a certain directory. The .htaccess file is often accompanied by a .htpasswd file which stores valid usernames and their passwords. With .htaccess you can customize error responses. You can do that changing the page that is shown when a server-side error occurs. Also, you can rewrite URLs. Various server-side PHP scripts use .htaccess to rewrite URLs to shorter ones.

Customizing error messages is easier than you think. Define the your custom pages in .htaccess using for example ErrorDocument 401 /authorization.html and upload them in www directory. The usual error codes are: 400(Bad Request), 401(Authorization Required), 402(Payment Required), 403(Forbidden), 404(Not Found), 405(Method Not Allowed), 406(Not Acceptable (encoding)), 407(Proxy Authentication Required), 408(Request Timed Out), 409(Conflicting Request), 410(Gone), 411(Content Length Required), 412(Precondition Failed), 413(Request Entity Too Long), 414(Request URI Too Long), 415(Unsupported Media Type), 500(Internal Server Error), 501(Not Implemented), 502(Bad Gateway), 503(Service Unavailable), 504(Gateway Timeout) and 505(HTTP Version Not Supported).

If you don't have an index file on your website and you want to prevent users from accessing it and to list all your files from that directory, add the following command to .htaccess: Options -Indexes. Also, you can restrict access to your website for a certain IP addresses using deny and allow commands. You can block an ip address this way: deny from 192.168.0.20. Also you can block a range of ip addresses specifying only 1 or 2 group of numbers. To allow access to an ip address, add the following command to .htaccess: allow from 192.168.0.20.

Another thing you can do with .htaccess is redirecting requests to certain files: Redirect /location/from/root/file.ext http://www.softpedia.com/new/file/location.xyz. Also you can redirect directories from a website to another: Redirect /softpedia http://www.softpedia.com/softpedia