Using a .htaccess file

Expertise level: Medium

What is a .htaccess file?

In the Apache web server, .htaccess (hypertext access) is the default name of directory-level configuration files.

The .htaccess file affect the directory where it is placed in as well as all it's sub-directories (an .htaccess file located in your root directory domain.com would affect domain.com/folder, domain.com/folder/contents/, etc.).

This can be prevented by placing a new .htaccess file within the directory which you want to preserve.

Here is a list of configurable elements with a .htaccess file:

  • Error Documents
  • Password protection
  • Enabling SSL
  • Blocking users by IP
  • Blocking users/sites by referrer
  • Blocking bad bots and site rippers (aka offline browsers)
  • Change your default directory page
  • Redirects
  • Prevent viewing of htaccess
  • Adding MIME types
  • Preventing hot linking of your images and other file types
  • Preventing directory listing

Here are a few examples:

Redirects to https:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^/?$ "https\:\/\/domain\.com\/" [R=301,L]

Custom error pages:

ErrorDocument 404 /home/user/404.html

Blocking an IP:

deny from 10.10.10.10

Prevent viewing of htaccess:

<Files .htaccess>
order allow,deny
deny from all
</Files>


For more information, please consult the official Apache documentation:

http://httpd.apache.org/docs/2.2/howto/htaccess.html

Have more questions? Submit a request

0 Comments

Article is closed for comments.
Powered by Zendesk