.htaccess
.htaccess

is a configuration file for use on web servers running the Apache Web Server software.

  • When a .htaccess file is placed in a directory which is in turn ‘loaded via the Apache Web Server‘, then the .htaccess file is detected and executed by the Apache Web Server software.
  • These .htaccess files can be used to alter the configuration of the Apache Web Server software to enable/disable additional functionality and features that the Apache Web Server software has to offer.
  • These facilities include basic redirect functionality, for instance if a 404 file not found error occurs, or for more advanced functions such as content password protection or image hot link prevention.
.htaccess
.htaccess

How to use .htaccess ?

  • ‘.htaccess’ is the filename in full, it is not a file extension.
  • For instance, you would not create a file called, ‘file.htaccess’, it is simply called, ‘.htaccess’. This file will take effect when placed in any directory which is then in turn loaded via the Apache Web Server software.
  • The file will take effect over the entire directory it is placed in and all files and subdirectories within the specified directory.
  • You can create a .htaccess file using any good text editor such as TextPad, UltraEdit, Microsoft WordPad and similar (you cannot use Microsoft NotePad).

hhtaccess Example :

AuthName “Member’s Area Name”
AuthUserFile /path/to/password/file/.htpasswd
AuthType Basic
require valid-user
ErrorDocument 401 /error_pages/401.html
AddHandler server-parsed .html

This is a fairly advanced example:

  • it enables password protection on the directory;
  • it offers redirection to a custom error page if a user fails to login correctly;
  • it enables SSI (server side includes) for use with ‘.html’ files.

Upload .htaccess file :

  • When uploading your .htaccess files, it is very important you upload the file in ‘ASCII’ mode. ‘ASCII’ and ‘BINARY’ are different methods of transferring data and it is important .htaccess files are transferred in ‘ASCII’ mode and not ‘BINARY’.
  • Upload the .htaccess file to the directory you would like it to take effect over.
  • Now visit this directory using your web browser as you would for any other document on your web site and check it has worked correctly.
  • Note, when you upload your .htaccess file it may not appear in the directory listings for files on your web site. Do not worry; this means your server or FTP software is hiding them which should not be an issue.

File Permission :

  • A possible cause of error is if the file permissions on the .htaccess file are not set correctly. This only occurs on certain servers, but you may like to change the permissions on the file to ‘755‘ or ‘executable’.
  • You can do this with your FTP software, look for a ‘File Permissions’ or ‘CHMOD’ option, and input ‘0755’.
  • If your .htaccess file does not work, you should contact your system administrator or web hosting company and ensure they have enabled .htaccess within your account.
  • Some web hosting companies do not allow use without permission. If errors persist, consult this article for advice, or contact your system administrator for advice.

Error Documents :

  • Creating custom error pages is very useful, it allows you to show web site visitors a friendly error message, for instance if a URL on your web site does not work. This avoids the unfriendly ‘404 File Not Found’ error and allows you to display a friendly error, explaining possible solutions and guiding the visitor back into your web site content, rather than leaving them frustrated and lost.
  • To set-up custom error documents, create a .htaccess file following the main instructions and guidance which includes the following text:
    ErrorDocument 404 /error_pages/404.html
  • The above line tells the Apache Web Server to display the document located at /error_pages/404.html (under your domain name/web site address) whenever a 404 (file not found) error occurs.In previous example, we have assumed you have created the error document and called it ‘404.html’ and that you have placed it in a directory called ‘error_pages’ under your domain name. For example, http://www.yourdomain.com/error_pages/404.html
  • The document 404.html is a normal HTML document like the others in your web site and can display whatever content you wish, however we recommend including a ‘File Not Found’ message.
  • To setup further error documents, for example for ‘401 Unauthorised’, ‘403 Forbidden’, and ‘500 Internal Server’ error messages, create a .htaccess file following the main instructions and guidance which includes the following text:
    ErrorDocument 401 /error_pages/401.html
    ErrorDocument 404 /error_pages/404.html
    ErrorDocument 500 /error_pages/500.html

Redirects using .htaccess file :

  • Redirects enable us to direct web site visitors from one document within your web site to another.
  • for example, if you have moved your web site content and would like to redirect visitors from old links to the new content location.
  • To set-up redirects, create a .htaccess file following the main instructions and guidance which includes the following text:Redirect /old_dir/ http://www.yourdomain.com/new_dir/index.html
  • The above line tells the Apache Web Server that if a visitor requests a documents located in the directory ‘old_dir’, then to display the document ‘index.html’ located in the directory ‘new_dir’.
  • you will also notice the location of the file that the visitor is to be redirected to is a full web site URL,
  •  it doesn’t have to be held within your web site content and could be any web site.
  • It is very important (and the most common cause of error) that you understand the difference between a relative URL and an absolute/full URL. A relative URL is the location of the document within the web site, and does not include the actual domain name of the web site. These are used for documents held within the web site to simplify and shorten the URL. A absolute or full URL is one which includes the full domain name.For example, for a absolute/full URL, ‘http://www.yourdomain.com/directory/file.html‘. the relative URL for this document would be, ‘/directory/file.html‘.

Password Protection using .htaccess :

  • The password protection and authentication systems offered by the Apache Web Server are probably the most important use of .htaccess files.
  • Very easily, we can password protect a directory (or multiple) of a web site which require a username and password to access.
  • The login procedure for these secure directories is handled automatically by the web browser using a pop-up login interface (you’ve probably seen these before).
  • Passwords are also encrypted using one of the best encryption methods available which ensures login credentials are kept secure.

To begin, decide which directory you would like to password protect (note that all files and subdirectories within the directory will be password protected), then create a .htaccess file following the main instructions and guidance which includes the following text:

AuthName “Member’s Area Name”
AuthUserFile /path/to/password/file/.htpasswd
AuthType Basic
require valid-user


Will be Continued, 

Source : htaccess-guide


Related Topics :

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.