The .htacess file is a configuration file that runs on website servers using Apache Web Server.  So prior to writing this file you need to confirm that your website server is using Apache or this file will not work.  The .htaccess file loads prior to your website loading and it essentially gives the web server instructions on how to behave.   Unless otherwise specified this file is usually uploaded into the root of your web files and you will need to change the file permissions to 644 for it to work properly.  

Creating a .htaccess file is really pretty easy to do.  I always use a text editor like notepad or an application that is equivalent.  I save the file as text file, close it, and then I go to the location of the text file and I make sure that the .txt extension has been removed to the final name of the file is ".htaccess" as you can see there is no extension on this file.  Now I open up the text file and I begin to add directives.  Htaccess files can do a lot of things but you have to know what you want it to do prior to adding instructions.  Also, keep in mind if your syntax is wrong it will probably cause an error and your website won't work properly.  I utilize the .htaccess file mainly for help with Search Engine Optimization, Page Re-directs and website speed.

Below are several examples of the directives that I utilize in my htaccess files.  Keep in mind there are a bunch of different ways to write directives, below is the way that I do it and I have found that for hand coded websites that utilize GoDaddy or Hostgator servers the code works well.  The coding examples below may not work with your website or with your web host, especially if you are using a content management system like Wordpress.  I have found sometimes web hosts require different directives and some CMS systems get squirrley as well.  This exercise will be a little trial and error so if your website breaks you will need to identify the code that is causing the problem.  If you try my examples below and it isn't working, feel free to add a comment at the bottom of the page and I will try to help point you in the right direction.    

Re-directs using .htaccess

Redirect 301 /oldFile.php

The above is example is what I would use if I wanted to change the name of oldFile.php to a new file name.  So the structure is essentially the same for these types of redirects.  Redirect 301 has to start each line, followed by a space, slash, oldFile name, space, and finally the full URL of where oldFile.php is going to be re-directed.  If you have multiple files that need to be redirected then start a new line for each file re-direct.

Redirect 301 /

The above redirect would allow you to redirect an entire website from the current location to a new domain name.

Page Speed directives with .htaccess

ExpiresActive On
ExpiresDefault "access plus 52 weeks"
ExpiresByType text/html "access plus 1 day"

The ExpiresDefault headers directs a browser to store all of of your website’s components (png images, svg images, jpg images, scripts, css files, etc.) in your browser’s cache until they expire.  Notice below the ExpiresDefault I'm changing the html expiration to just one day which can be important for content that loads dynamically or frequently changes.  Next we are defining how long to keep these files from expiring,  I generally use 52 weeks.  I useone year in the future, so the browser doesn't have to request any file types they are automatically loaded from the browsers cache if the resources have been cached.  This will not only make your page load faster but it will also decrease the strain on your server.  Please be advised that this directive requires your host to have enabled mod_expires (Usually this is done by default on most web hosts). 

AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

The above code is taking advantage of Gzip compression.  Gzip is a method of compressing files which makes loading those files from a server much faster.  On many occasions by using Gzip the size of your web files will be reduced up to 75% of it's original size which makes them much faster to load.  Be advised on different web server setups the above code may or may not work.  Therefore, I'm going to leave a link to a post that goes into a great deal of detail on other Gzip options.  

Header unset ETag
FileETag None

An entity tag (ETag) is an HTTP header used for Web cache validation and conditional requests from browsers for resources. Etags use persistent identification elements (PIE) that have been tagged to the user's browser.  (definition cite)   In most instances, sending ETag headers will negatively impact the load performance of your site.  I can't give you the specifics but if you want to learn more about Etags feel free to vsit Wikipedia

AddDefaultCharset UTF-8

To display an HTML page correctly, a web browser must know which character set (character encoding) to use.  The standard across the web is UTF-8, because it covers almost all of the characters and symbols in the world.  We need to tell the browser which character set to use so we don't leave it up to the browser to guess.  You can either add a meta tag to each individual page or you can add a directive to your htaccess file.  I normally add it to the htaccess file to ensure no pages get missed.  This instruction will speed up your page because if UTF-8 isn't specified the browser needs to scan the page and decide what character encoding to use.  By telling the browser ahead of time it takes a step out of the page loading process. 

Lastly, there is a cool Apache module called mod_pagespeed and it is an opensource project created by Google.  This module is automatically included in some web hosting services and it has some great features that should also improve page load speed.  I'm not going to discuss this as an option, because I use Cloudflare as a content delivery network and this directive is mostly redundant with Cloudflare.  If you are interested in learning more about it consider going here.

Search Engine Optimization using .htaccess

RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$$1 [R=301,L]

My website will load and  I don't want Google to think these are two separate pages so the above directive tells Apache to serve all URL's as  I utilize the above directive along with rel=canonical tags to make sure that this is accomplished but the rel=canonical tag is another discussion.  To be clear you only need to utilize the code RewriteEngine and RewriteBase one time then you can add any rewrites below the RewriteBase line.  As you will see in the code block below I'm not utlizing either of these lines because I always include the following code braces below, the code block above.  

RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301]

It is important that all of your pages serve consistently from Google.  Some people like to use other people like to use  In the end I like to use the www. version mainly because Google and a lot of other big name companies do, but some people like using the non-www version of the site.  The key is from a search engine optimization perspective you need to choose one and stick with it.  Don't confuse Google by using both because this will hurt your search rankings.  Either way you choose is up to you, but the above code tells the site to rewrite all URL's to 

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)? [NC]
RewriteRule \.(jpg|png|gif)$ - [NC,F,L]

Image Hotlinking allows spammers to use your server to load images for them.  One spammer hotlinking your images won't be that big of a deal, but the more that do it to your site, the more you will notice your site bogging down.  Hot linking taxes your server and slows the speed of your site down so we want to keep spammers from doing this and that is why we utilize the directive above.  This is essentially apart of the page speed section above, but I put it here because I put this code below the one time use of RewriteEngine and RewriteBase one time. 

That is how I use my htaccess file with real code examples that I utilize on my sites.  If you feel I've left some good options out then please post a comment below and maybe you can teach me something.   

If you are looking for help with htaccess file and you live in Erie or Longmont Colorado feel free to give us a ring.