apache tutorial - What is .htaccess? - apache - apache web server - apache server - apache2




  • Apache server software provides distributed (i.e., directory-level) configuration through Hypertext Access files.
  • These htaccess file enable the localized fine-tuning of Apache’s universal system-configuration directives, which are defined in Apache’s main configuration file.
  • An .htaccess file is best used,
    • to code website errors (like 404 or 301 page errors),
    • URL rewrites, or
    • directory permissions.
    • pointing to new locations of old content,
    • creating an error page for content that no longer exists
    • requiring a password to access certain files or directories on the server,
    • pointing complex numerical URLs to more simplistic permalinks using software like WordPress.

htaccess location : example of wordpress htaccess file shown below,

  • htaccess apache rules apply to the parent directory and all subdirectories. Hence, to apply configuration rules to an entire website, place the .htaccess file in the root directory of the site.

Commenting apache htaccess Code:

  • Comments are essential to maintaining control over any involved portion of code in htaccess apache.
  • Comments in .htaccess code are fashioned on a per-line basis, with each line of comments beginning with a pound sign #.
  • The comments spanning multiple lines in the .htaccess file require multiple pound signs.

htaccess example

Using apache htaccess files:

  • An htaccess file is a plain-text configuration file that enables to customize the web server's configuration.
  • When the Apache web server processes an incoming client request for a file, it looks for an htaccess file in the same directory.
  • If the .htaccess file exists, apache htaccess reads the directives in the file and overrides the server's global configuration.
  • Any directives defined in an .htaccess file apply to the directory where the file is located, and to all subdirectories below it.
  • we can use directives in .htaccess files to redirect requests to different URLs, control directory listings, specify custom error documents, and etc.,

Protecting .htaccess file contents:

  • By default, anyone can view the contents of an .htaccess file.
  • However, this is a potential security risk, because it exposes web site configuration information.

.htaccess secure options :

  • For security reasons, it is a good idea to prevent visitors from viewing .htaccess files.

Add the following directives to the .htaccess file:

Now, users who try to view the .htaccess file, will directly receive htaccess 404 error in their web browser.

Related Searches to What is htaccess