Basics of htaccess files

Rakesh Sharma      No Comments  

Basics of htaccess files :-

Today every site need to attract users to do this one more need to make url search engine friendly by url rewriting. Url rewriting done by htaccess file. Also by .htaccess you can make protection, allow/deny directory and more of use. In this tutorial i am sharing only basics of htaccess files.

What is htaccess? :-

A .htaccess is a configuration file which provides configuration changes on a directory level basis. Mostly used for url rewrite, restrict directory contents, password protection etc.

Full Name :- HyperText Access or HTTP Access

Requirements :- Mod rewrite must be enabled on your server.(you can check this by phpinfo()). Default it’s enabled on all hostings. if not enabled you will get 500 Internal server error.

Basics of htaccess files

Using .htaccess :- Now we need to learn how to use htacess. for this we need to create a .htaccess file keep name simple “.htaccess“. mostly placed on site root so upload it via FTP or any file transfer tools. if we have added some rewrite rules in it will effect all directories and subdirectories.

Basic syntax used in htaccess :-

1. Rewriting :-

RewriteEngine on
RewriteBase /
RewriteRule ^([^/]+)/?$ /my.php?id=$1 [L]

RewriteEngine On – For enabling rewrite rules, add only once else rules will be ignored.
RewriteBase – To define directory from apply rules.
RewriteRule – Applying a rule with pattern and flag.

2. Comments :- comments in .htaccess usually done by “#” sign

#RewriteRule ^([^/]+)/?$ /my.php?id=$1 [L]

3. Checking for module enabled :- check work to check if a module is enabled for applying rules. if we use checking block then rules will apply if module enabled else no exceution of rules.

<ifmodule mod_deflate.c>
// add your rules here

4. Allow/Deny :- allow to allow a directory or site access from ip/url. deny to deny access.

order allow,deny
Deny from 209.185.108
Deny from 209.185.253
allow from all

you can not access site with blocking ip’s and allow for all other.

To deny access a directory add below code to .htaccess :- you can not access this directory by url.

Deny from all

There are more to know about .htaccess but in this tutorial we have discussed some basics of htaccess files for more you can check htaccess guide.