.htaccess File


.htaccess File Banner

What is .htaccess File?

An .htaccess file is a powerful website file that controls high-level configuration of website. An .htaccess file is a configuration file used by the Apache web server software, which allows to make changes to website’s configuration without having to edit server configuration files. This means that .htaccess files use to configure options for specific directories on web server, rather than configuring them globally for all directories.


An .htaccess files are placed in the root directory of website, and the configurations set in the file apply to all subdirectories underneath it. An .htaccess files can also create in subdirectories to override the configurations set in the root directory.


Uses of .htaccess File

  • Setting custom error pages: .htaccess use to set custom error pages for website. For example, "404 Page Not Found" error page can create by .htaccess file and .htaccess use to redirect users to that page when they try to access a page that doesn't exist on website.
  • Redirecting traffic: .htaccess use to redirect traffic from one page to another. For example, .htaccess use to redirect all traffic from an old page to a new page on website.
  • Setting password protection: .htaccess use to set password protection for specific directories on website. This can be useful for creating private areas of website that are only accessible to certain users.
  • Blocking specific IP addresses or referrers: .htaccess use to block specific IP addresses or referrers from accessing website. This can be useful for blocking malicious users or bots.
  • Setting up rewrites: .htaccess use to set up rewrites, which can be used to change the appearance of URLs on website. This can be useful for creating clean and user-friendly URLs.
  • Setting caching options: .htaccess use to set caching options for website. This can help to improve the performance of website by allowing the web server to cache static content.
  • Setting up MIME types: .htaccess use to set up MIME types for website. MIME types means the type of content being served, such as HTML, JPEG, or MP3.

These are just a few examples of the many things can do with .htaccess files. The specific options available, will depend on web server and the version of Apache are using.


How make .htaccess File?

To create an .htaccess file, follow these steps:

  • Open a text editor, such as Notepad or TextEdit.
  • Type the desired configuration options into the text editor.
  • Save the file as .htaccess (including the period at the beginning). Be sure to save it as a plain text file.
  • Upload the file to the root directory of your website.

Examples of .htaccess file.

  • Redirecting traffic from example.com to www.example.com: RewriteEngine On
    RewriteCond %{HTTP_HOST} ^example\.com$
    RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

  • Setting a custom 404 error page: ErrorDocument 404 /errors/404.html
  • Setting password protection for a directory: AuthType Basic
    AuthName "Restricted Area"
    AuthUserFile /path/to/.htpasswd
    Require valid-user

  • Blocking specific IP addresses: order allow,deny
    deny from 1.2.3.4
    deny from 5.6.7.8
    allow from all

  • Setting up rewrites: RewriteEngine On
    RewriteRule ^products/([0-9]+)/([a-z-]+)$ /products.php?id=$1&name=$2 [L]

  • Setting caching options: ExpiresActive On
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType text/css "access plus 1 week"

  • Setting up MIME types: AddType application/x-httpd-php .php
    AddType video/mp4 .mp4

These are just a few examples, and the specific options available, which will depend on web server and the version of Apache.