Monday, August 25, 2008

Simple htaccess Tutorial

What is .htaccess?

In the Apache web server, .htaccess (hypertext access) is the default name of directory-level configuration files. A .htaccess file is placed in a particular directory, and the directives in the .htaccess file apply to that directory, and all subdirectories thereof. It provides the ability to customize configuration for requests to the particular directory. The file name starts with a dot because dot-files are by convention hidden files on Unix-like operating systems.

The .htaccess file is a simple text file (in ASCII format). In general you can use, .htaccess files to change some configuration directives of the apache web server. However, .htaccess files can do lot more than simply change some settings of your web server as you will find from the topics listed in this tutorial.
Please, note that the name of the file is “.htaccess”. It starts with a dot. By default on Unix/Linux systems files starting with a dot are not publicly visible. If you are running a Windows PC, it is likely that you will have troubles creating such a file. So if you need to create such a file, create it directly on your web hosting server and modify it there, using your FTP program, SSH or some web based file manager.

The .htaccess file can be placed on any folder on your site. It has recursive effect. This means that if you place the .htaccess file in your web root (the main folder of your web site) the directives and commands you place in the .htaccess file will have effect on all sub-folders.
If you place a .htaccess file in a sub-folder, its directives will override the ones that you have in your site main folder. That is if you disable directory listing globally for your site by placing the proper line in the .htaccess file in your main folder, you can then enable directory listing only for a particular sub-folder with another .htaccess file and the proper directive.

Redirect URL using :

The Apache web server provides several way for setting up redirects.

The most simple one is using the “Redirect” directive:

Redirect /folder http://www.example.com/newfolder

With such a line in your .htaccess if a visitor tries to load http://www.example.com/folder, he will be redirected to http://www.example.com/newfolder.

Recently it has been talked a lot about Permanent redirects. The good news is that you can add a status code to the Redirect directive. For example for Permanent 301 redirect you can use:

Redirect permanent /folder http://www.example.com/newfolder


Another useful directive is the RedirectMatch. With it you can use regular expressions in the redirect condition. For example

RedirectMatch "\.html$" http://www.example.com/index.php

This will redirect all requests to files that end with .html to the index.php file.

There is another more powerful way to create redirects or even create transperant redirects which requires ModRewrite. We will talk about this in the next article.

How to change error documents?

let’s start with most common error page. The 404 error page:

ErrorDocument 404 /notfound.html

The line above tells the webserver to use a file named missing.html as error document.
Please note, the leading slash. It tells the browser that this file is located in your web site root folder. In case you miss the slash the webserver will look for a missing.html in the current directory. In case you do not have such a file a default 404 page will be server with a message that an additional error has occurred while trying to find the missing.html file you have defined in your .htaccess file.

If you wish to keep your site structure clean and organized, you can create a folder on your website and keep all custom error pages there. For example you can create a folder named “errors” and place all pages that are going to be used as error handlers there. With a subdirectory your .htaccess file will look like

ErrorDocument 404 /errors/notfound.html

So, now let’s have an example with some more error codes:

ErrorDocument 500 /internal_error.html
ErrorDocument 401 /authorization_required.html
ErrorDocument 403 /forbidden.html

As you can see from the example the ErrorDocument uses a simple syntax:

ErrorDocument

Where should be replaced with the HTTP error you should assign a custom error page and the should be replaced with the path to your own custom error page.

Here it is list of some common HTTP error codes:


400 Bad Request
The server received a request it cannot handle due to bad syntax for example

401 Unauthorized

Such an error will show up in case a user did not supply a proper login credentials when using the .htaccess based user/pass protection

403 Forbidden
The request page is forbidden. Such an error shows up when you have a Deny from directive

404 Not Found
As the error message says the page that you have requested cannot be found on the server.

410 Gone
The requested page have been removed permanently

500 Internal Server Error
The server encountered an error. Usually such error messages show up with CGI scripts. Also you can get such an error message when you have bad syntax in your .htaccess file.

Top Tutorials in htaccess
-

No comments:

Popular Posts