What is htaccess?


.htaccess
(hypertext access) is the default name of a directory-level configuration file that allows for decentralized management of web server configuration, with htaccess we able to override a subset of the server's global configuration. The original purpose of .htaccess was to allow per-directory access control (e.g. requiring a password to access the content), hence the name.

How to configure .htaccess ?
If you want to call your .htaccess file something else, you can change the name of the file using the AccessFileName directive. For example, if you would rather call the file .configthen you can put the following in your server configuration file:

AccessFileName .config
In general, .htaccess files use the same syntax as the main configuration files. What you can put in these files is determined by the AllowOverride directive. This directive specifies, in categories, what directives will be honored if they are found in a .htaccess file. If a directive is permitted in a .htaccess file, the documentation for that directive will contain an Override section, specifying what value must be in AllowOverride in order for that directive to be permitted.
For example, if you look at the documentation for the AddDefaultCharset directive, you will find that it is permitted in .htaccess files. (See the Context line in the directive summary.) The Override line reads FileInfo. Thus, you must have at least AllowOverride FileInfo in order for this directive to be honored in .htaccess files.
For example configure the test folder .htaccess
  • We will need to create the folder that will have to be authenticated. Since the default location in apache is /var/www/html we will create it here. You will do this by using the mkdir command.
    [root@linux ~]# mkdir /var/www/html/testfolder
  • Next we need to add the .htaccess & .htpasswd files to the personal folder. We first need to change the directory of the folder we wish to protect.
    [root@linux ~]# cd /var/www/html/testfolder
  • Next we can create the .htaccess file.
    [root@linux ~]# vi .htaccess
  • Press i to insert and add the following content.
    AuthUserFile /var/www/html/testfolder/.htpasswd
    AuthGroupFile /www.null
    AuthName "Authorization Required"
    AuthType Basic
    
    require user USER_NAME
  • Change "test folder" to the name of your folder and change "USER_NAME" to the user name you wish to use.
  • Press your esc button then :wq to save your file in your vi editor.
  • Next we'll create the .htpasswd file. We want to run htpasswd on the path of the folder we want to protect.
    [root@linux ~]# htpasswd -c /var/www/html/testfolder/.htpasswd USER_NAME
  • You should see something like this:
    [root@linux ~]# htpasswd -c /var/www/html/testfolder/.htpasswd USER_NAME
     New password:
     Re-type new password:
    Adding password for user USER_NAME
  • Verify that these file were created in your testfolder directory. To do this use "ls -a" in your command prompt.
  • Next we will have to edit the apache httpd.conf (on some systems called the apache2.conf) file.
    [root@linux ~]# vi /etc/httpd/conf/httpd.conf
  • You will have to scroll all the way to the bottom to add the following directory.
    #FOR MY TEST FOLDER
     <Directory "/var/www/html/testfolder">
     AllowOverride AuthConfig
     </Directory>
  • Finally save httpd.conf by typing esc :qw! and restart apache.
    [root@linux ~]# service httpd restart

0 comments:

Post a Comment