Hypertext Access, commonly shortened to htaccess, it is used to control the directory it is placed in and all the subdirectories underneath it and it is also used for friendly url.Htaccess file should be placed in your root directory only then it can be accessed.

Basic Rules Of .htaccess

  1. Htaccess files must be uploaded as ASCII mode, not BINARY.
  2. You will need to CHMOD the htaccess file to 644 or (RW-R–R–) so that the file is usable by the server (and prevents it from being read by a browser).
  3. Commands in htaccess are mostly single line and you may need to turn off auto word wrap.
  4. htaccess will also affect sub directories in which they are placed.

.htaccess file priorities

  1. A lower-level .htaccess file will override the settings in a higher-level one. For example, if you want to forbid directory indexing for most of your site but enable it for a specific directory, you can upload your .htaccess file to just that directory.
  2. Similarly, .htaccess files override PHP settings from your higher-level vhost.conf and/or php.ini files, unless overriding is disallowed in these higher-level files. This means that you can set a PHP upload limit for one of your domains higher than it is on the rest of the server.
  3. Finally, the order of directives within a file can matter for certain types of directives. Directives at the top are processed first.

Creating .htaccess files:

Step 1: ON the rewrite engine.

 

<IfModule mod_rewrite.c>
RewriteEngine on
</IfModule>

Step 2: Write the rewrite conditions.

Example :

RewriteCond %{HTTP_HOST} ^siriinnovations.com [NC]

Here if you write siriinnovations.com it will redirect to www.siriinnovations.com & if you write http://www.siriinnovations.com it will redirect to www.siriinnovations.com

Step 3: We can write our own rewrite rules

Syntax:

Rewrite ([a-zA-Z0-9.=^%$#!~@,]+)/([Rule ^yourname$ orginialfilename [QSA,L]

RewriteRule ^audiovideo$ audiovideo.php [QSA,L]

RewriteRule ^contact/([0 -9]+)/([a-zA-Z]+)$contact.php?id=$1&$name=$2 [QSA,L]

In above the example were sending two paramaters.
Note: We can send any number of parameters there are no restrictions.

Pregmatch conditions in .htacess files:

  1. [a-zA-Z]) -It allows only small and capital alphabets .
  2. [0-9 ]- It allows only numbers.
  3. [a-zA-Z0-9_-]  -It allows only small and capital alphabets, numbers and special characters like (_,-)
  4. [a-zA-Z0-9_-\s] – It allows only small and capital alphabets, numbers and special characters like (_,-,space)
  5. [a-zA-Z0-9.=^%$#!~@,{)(] – It allows only small and capital alphabets, numbers and special characters like (_,-,space,=,.,^,%,$,#,<,! ,~,@,{,),(,])