.htaccess Tutorials


What is htaccess ?

The .htaccess is a configuration file for use on web servers, it is supported by Apache Web server and others several webservers. An .htaccess (hypertext access) file is a directory-level configuration file,it is hidden file in Unix-based environments. .htaccess file causes server performance

What is the use of .htaccess file ?

The following use of .htaccess file.

  • URL redirection
  • URL shortening
  • Access control (for web pages and files)
  • SSI
  • MIME types

Alternative Index Files


Scripts as Source Code

Always script is executed on server but you want to view as source code or you want to directly download. By .htaccess file you can do by spripping the script handler for particular types of file.

#view as source code
RemoveHandler cgi-script .php .py
AddType text/plain .php .py

Force Download by MIME Type

MIME : Multipurpose Internet Mail Extensions, For the directive there are three parts :

  • AddType command
  • MIME type
  • The file extension.
#These file extensions to download by default
AddType text/plain .php .py
AddType application/octet-stream pdf

Enabling CGI

CGI(Common Gateway Interface) is a server-side method, it is used to includes non-HTML scripts like Perl, SSI , etc

AddHandler cgi-script .cgi
Options +ExecCGI 

Disallow Script Execution settings

Options -ExecCGI
AddHandler cgi-script .php .py .cgi

Enabling or Disabling an Index

When in directory with documents but no index.html file then all files name will display of director so You used enabling an Index

Enabling Indexes

Options +Indexes

Disabling Indexes

Options -Indexes

Keeping the some files hidden from the Index

#To show all files except pdf and png files
IndexIgnore *.pdf *.png

#To show all files except test.png file
IndexIgnore test.png

PHP Settings

You can change the php.ini settings also by .htaccess file.

#sytanx
php_value [setting_name] [value]

#Example
php_value post_max_size 30M
php_value upload_max_filesize 40M