Knowing a little htaccess can go a long way when optimising a site.
love htaccess
When configured correctly these all contribute to a healthy performing website with an increase in speeds, for both the server and the user, and keeping those crawling errors at bay whilst bringing in smiles from the search engines – everyone is happy.

The htaccess file is most commonly used with Apache servers and used for configuring access to your website. Typical usage for htaccess includes rewriting/redirecting urls, producing custom error pages, sorting MIME types, browser caching and even security.

One of the most common features you will need in your arsenal as an SEO is to be able to use redirects and rewriting URLs. But there are some other common commands, and not so common ones, which you might not be aware of, some of which are listed below.

  • 301 Redirects
  • Non www to www 301 Redirect
  • Www to non-www 301 Redirect
  • Stripping Multiple Slashes
  • Browser Caching
  • File Compression
  • Error Documents

Always test to destruction from what you have added. Use crawling software such as Screaming Frog SEO Spider Tool to check for errors, and use an online header status checker to see if redirects are working as expected also try using Google’s Page Speed Tool to compare if your improvements have worked.

Warning! Before proceeding with any changes to your htaccess file it is always wise to make a backup and test it locally on a machine before implementing them on your own website.

301 Redirects

Redirect your pages the search engine friendly way.

Below are the most common redirects you will use, especially for SEOs, using the 301 redirect which tells search engines this is the permanent new location for the page or resource. You will commonly have to do this, for example, from spotting crawling issues in Google Webmaster Tools or you have an old page that needs redirecting to a new location.

Insert the code in your htaccess swapping yourdomain.co.uk to your own domain.

These examples assume that RewriteEngine is On…

Redirect 301 /oldpage.html http://www.yourdomain.co.uk/newpage.html

Or if you are experiencing problems with the url you want to redirect ending in a forward slash then try…

RewriteRule ^oldpage/ http://www.yourdomain.co.uk/newpage/ [R=301,L]

Non www to www 301 Redirect

Redirect the non-www version of your site to the www version.

One of the most common redirects you will find when approaching a site in SEO.

This snippet is useful for redirecting all your pages to the www version of your site using a 301 redirect – a legitimate redirect that search engines recognise.

It’s also handy in stopping users from linking to the non-www version of your content on your site. Add this piece of code to your htaccess, it should be one of the first redirects your site makes so place it high in the file. This snippet assumes that RewriteEngine is On…

eg. http://yourdomain.co.uk/ to http://www.yourdomain.co.uk/

### Non-www to www redirect ###
RewriteCond %{HTTP_HOST} ^yourdomain.co.uk [NC]
RewriteRule ^(.*)$ http://www.yourdomain.co.uk/$1 [R=301,L]
### EO Non-www to www redirect ###

Non www to www 301 Redirect

… Or perhaps your site is catered to the non-www version. This is a search engine friendly way to redirect from the www version to the non-www version of your site…

eg. http://www.yourdomain.co.uk/ to http://yourdomain.co.uk/

### Www to non-www redirect ###
RewriteCond %{HTTP_HOST} ^www.yourdomain.co.uk$ [NC]
RewriteRule ^.*$ http://yourdomain.co.uk%{REQUEST_URI} [R=301,L]
### EO www to non-www redirect ###

Stripping Multiple Slashes

This handy little snippet removes all those extra slashes in one hop with a 301 redirect.

Sometimes in Google Webmaster Tools or using a tool for crawling your site you might find a few strange URLs with multiple slashes where someone has somehow linked to a page in the format http://www.example.com///path///here/// rather than the preferred format of http://www.example.com/path/here/ (weird, but it does happen).

In most cases you will find that the server isn’t handling these URLs correctly and displaying the page as if normal. If the code is an internal link on your site then you should update it removing the multiple slashes, but if someone is linking to you and you are unable to update their link here is one solution:

This snippet assumes that RewriteEngine is On…

### STRIP MULTIPLE SLASHES ###
RewriteCond %{REQUEST_URI} ^/([^/]+)//+(.*)$
RewriteRule / http://%{HTTP_HOST}/%1/%2 [R=301,L]

RewriteCond %{HTTP_HOST} !=""
RewriteCond %{THE_REQUEST} ^[A-Z]+s//+(.*)sHTTP/[0-9.]+$ [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+s(.*/)/+sHTTP/[0-9.]+$
RewriteRule .* http://%{HTTP_HOST}/%1 [R=301,L]
### EO STRIP MULTIPLE SLASHES ###

The above code should be placed near the top of the file as one of your first main redirects after any non-www to www (or vice versa) redirects.

Browser Caching

Enable caching to store frequently used files for a faster browsing experience.

Browser caching is an essential for both server performance and page speed for users. With caching there are far less calls to the server and users appreciate faster loading pages.

The below handles most file types from images, Flash, PDF’s to Javascript and CSS. Updating the expiry time is easy, make sure to remove any excess lines that might not be appropriate for your website.

The below assumes that the Mod_Expires module is installed.

### EXPIRES CACHING ###
<ifModule mod_expires.c>
ExpiresActive On

ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType application/javascript "access 1 month"
ExpiresByType application/x-javascript "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</ifModule>
### EO EXPIRES CACHING ###

File Compression

Serve smaller-compressed files for faster loading pages.

Compressing files makes life easier on download speeds for the user. When pages and resources are accessed the server compresses the files and sends them back to the user for their browser to unpack and display.

It is best to enable compression on a server by default but sometimes a host doesn’t give this access. If htaccess is enabled here are some commands to get around that…

### Compress File Types ###
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/x-javascript

# Watch out for legacy browsers
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
### EO Compress File Types ###

Error Documents

Serve default error pages for server errors or resources that do not exist

Setting up a default page to be displayed for server errors on htaccess is easy. Add the following line for setting up a page for 404 errors. Remember to create and style the page! These are good for orientating users and search engines to your most important pages or signalling what is wrong.

For 404 pages add the following to your htaccess…

ErrorDocument 404 "/404.php"

Simply change the number to the server error of your choice and construct a new page on the root of your host.

ErrorDocument 403 "/403.php"

What to expect…

From using some of the basics above on a htaccess enabled server you should see improvements to performance straight away. For SEO, expect to reap the benefits in the long term. Remember, always to check redirects you have created and any optimisations you have made are working as intended.

For more on optimising your website for speed check out Yousaf’s post on 17 rules of speed optimisation.