.htaccess Tutorial for Beginners
Posted by - NA - on 16 September 2008 07:44 PM
|
|
This tutorial will demonstrate how to use .htaccess files to perform several basic Apache and PHP configurations at the directory level. This tutorial makes the following assumptions:
If you're not sure about whether these statements are true, check with your web host's technical support department. There several types of configuration that can be performed using a .htaccess file. These include authorization, redirects, changing the index file name, displaying custom error pages, and changing PHP configuration settings. To perform such configuration, simply create a file named .htaccess (the dot in front is required) in the folder where you would like the settings to be applied. Any settings you configure for this directory will also be applied recursively to all of its sub directories. Note that some FTP clients may be configured to hide files that start with a dot. If this is the case, you may need to reconfigure your FTP client to display these files. Changing the index file nameThe index file is the file that gets displayed automatically when a user browses to a directory. Historically, the index file is called index.html or index.htm. On a PHP powered site, you may want your index file to be named index.php. This can be accomplished by putting the following line in your .htaccess file:
DirectoryIndex index.php index.html
The directive above instructs Apache to use index.php as the index file if it exists, otherwise it should look for a file named index.html. If neither file exists in the requested directory, the user will usually get a directory listing. Using a custom error pageEveryone at one point or another encounters the dreaded "HTTP 404 - File Not Found" error message. Perhaps the user followed a broken link, or mistyped the URL of the page they were looking for. The default error page is usually quite ugly. However, it is possible to use a custom error page for this or any other HTTP error. This can be done by placing the line below in your .htaccess file: ErrorDocument 404 http://www.mysite.com/404.html Replace "http://www.mysite.com/404.html" with the URL for the page you want the user to see when they request a missing file. It is important that your custom "file not found" page returns the HTTP 404 status code, otherwise your site could be penalized by the search engines. You can do this by adding the following PHP code at the top of your 404 page. <?php Denying access to a directorySometimes, there may be directories on your website that the user shouldn't be able to directly request files from. For example, you may have a directory that stores data files for your scripts, or a set of PHP includes. Placing the lines below in the .htaccess file for that directory will block direct requests for those files: Order Deny,Allow The first line ensures that the deny directive is evaluated before any allow directives that may have been defined elsewhere in the directory hierarchy. Redirecting requestsOver time, files on a website may get moved or renamed. Rather than letting the user to see a 404 error message when requesting the old file, you can redirect them to the new file using the following directive: Redirect permanent /old.php http://www.mysite.com/new.php
The PHP configurationLastly, .htaccess files can be used to change PHP settings when
running as an Apache module. Such configuration is performed using the php_flag register_globals off The following directive can be used to turn off magic quotes for all PHP scripts in a directory: php_flag magic_quotes_gpc off To enable or disable error reporting, use the php_flag display_errors on
To hide error messages from the end user, simply disable the php_flag display_errors off | |
|