".htaccess" Basics

Thursday, November 18, 2010
What is .htaccess?

Wikipedia says: In several web servers (most commonly Apache), .htaccess (hypertext access) is the default name of a directory-level configuration file that allows for decentralized management of web server configuration. The .htaccess file is placed inside the web tree, and is able to override a subset of the server's global configuration; the extent of this subset is defined by the web server administrator. The original purpose of .htaccess was to allow per-directory access control (e.g. requiring a password to access the content), hence the name. Nowadays .htaccess can override many other configuration settings, mostly related to content control, e.g. content type and character set, CGI handlers, etc.

Where or when to use it?

Authorization, authentication
.htaccess files are often used to specify the security restrictions for the particular directory, hence the filename "access". The .htaccess file is often accompanied by a .htpasswd file which stores valid usernames and their passwords.
Rewriting URLs
Servers often use .htaccess to rewrite long, overly comprehensive URLs to shorter and more memorable ones.
Blocking
Use allow/deny to block users by IP address or domain. Also, use to block bad bots, rippers and referrers.
SSI
Enable server-side includes.
Directory listing
Control how the server will react when no specific web page is specified.
Customized error responses
Changing the page that is shown when a server-side error occurs, for example HTTP 404 Not Found.
MIME types
Instruct the server how to treat different varying file types.
Cache Control
.htaccess files allow a server to control caching by web browsers and proxies to reduce bandwidth usage, server load, and perceived lag.

Since .htaccess is a hidden system file please make sure your FTP client is configured to show hidden files. This is usually an option in the program's preferences/options.

How do I start?
1. Create an empty text file using a text editor such as notepad, and save it as htaccess.txt.
NOTE:The reason you should save the file as htaccess.txt is because many operating systems and FTP applications are unable to read or view .htaccess files by default. Once uploaded to the server you can rename the file to .htaccess.
2. Edit the contents of the file. Check the following examples:
  • Point an entire site to a different URL, such as domain.net redirected to domain.com:

    # This allows you to redirect your entire website to any other domain
    Redirect 301 / http://rv8820.blogspot.com/

  • Redirect index.html to a specific subfolder:

    # This allows you to redirect index.html to a specific subfolder
    Redirect /index.html http://rv8820.blogspot.com/newdirectory/

  • Redirect an old file to a new file path:

    # Redirect old file path to new file path
    Redirect /olddirectory/oldfile.html http://rv8820.blogspot.com/newdirectory/newfile.html

  • Redirect to a specific index page:

    # Provide Specific Index Page (Set the default handler)
    DirectoryIndex index.html

  • Redirect users to access the site without www:

    # To redirect all users to access the site WITH the www. prefix,
    # (http://example.com/... will be redirected to http://www.example.com/...)
    # adapt and uncomment the following:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.rv8820.blogspot.com\.com$ [NC]
    RewriteRule ^(.*)$ http://www.rv8820.blogspot.com/$1 [L,R=301]

  • Redirect viewers of your domain to use the secure version of your domain:

    # An easy to way to always redirect the user to secure connection (https://) can be accomplished with the following lines:
    RewriteEngine On
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://www.rv8820.blogspot.com/$1 [R,L]

  • Redirect users to www for http:// and https://

    RewriteEngine On

    RewriteCond %{SERVER_PORT} 80
    RewriteCond %{HTTP_HOST} !^www\.rv8820.blogspot\.net$ [NC]
    RewriteRule ^(.*)$ http://www.rv8820.blogspot.com/$1 [L,R=301]

    RewriteCond %{SERVER_PORT} 443
    RewriteCond %{HTTP_HOST} !^www\.rv8820.blogspot\.com$ [NC]
    RewriteRule ^(.*)$ https://www.rv8820.blogspot.com/$1 [L,R=301]

  • Redirect users to use https:// for a particular folder:

    # In case you wish to force HTTPS for a particular folder you can use the following:
    RewriteEngine On
    RewriteCond %{SERVER_PORT} 80
    RewriteCond %{REQUEST_URI} somefolder
    RewriteRule ^(.*)$ https://www.rv8820.blogspot.com/somefolder/$1 [R,L]


3. Now upload the file to your root folder of your site and be sure to rename it to .htaccess without any filename extension.
4. Make sure that your redirect functions properly.
  • Paths to where you should save this file can be found in this article: System Paths
  • The definitive guide on Apache directives that can be used in .htaccess files can be found here: http://httpd.apache.org/docs/mod/core.html

Kindly Bookmark and Share it:


0 comments: