Making the most of .htaccess

htaccess, SEO & everything between

A .htaccess file is one of the files that controls the server. It tells the server (Apache) what informaiton to provide. By adding different lines of code to it you can make massive improvements to your websites.

It is important to create a backup of your .htaccess file before making any modification to it. Putting the incorrect code into the .htaccess file can cause your entire website to stop working.

Expires

The following peice of code tells the browser that it does not need to download the files for your website every time you visit. It helps improve the spped of your website in two ways. First of all by speeding up your page load speed, it does not need to download as many files. Secondly by reducing the stress on your server, you server is not having to generate files as the clients as often. Again allowing your server to generate the files up to new users.

# BEGIN EXPIRES
ExpiresActive On
ExpiresDefault "access plus 10 days"
ExpiresByType text/css "access plus 1 week"
ExpiresByType text/plain "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 week"
ExpiresByType application/x-icon "access plus 1 year"
# END EXPIRES

Rewrite

The next bit of code does not make your website any faster, but changed what files it uses to generate the webpage, and it is the code that WordPress run on! I tells the server to return the same file each time. The file then decided what content should be generated base on the URL, cookies and other variables. It also allows you to dynamically serve up a 404 page.

# BEIN REWRITE
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END REWRITE

GZIP

There are other ways that .htaccess can speed up your website. One of these ways is by allowing you to compress the files. This is done with a module called mod_gzip. By adding the code below, you tell the server to serve up files compressed. It uses GZIP compression and will only serve it in that compression if the browser can understand it.

Gzip for htaccess icon

# BEGIN GZIP
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
#END GZIP

Graphics created by Ralf Appelt