14 golden rules for site optimization

golden rules

1: Minimize HTTP Requests

80% of the end-user response time is spent on the front-end. Most of this time is tied up in downloading all the components in the page: images, stylesheets, scripts, Flash, etc. Reducing the number of components in turn reduces the number of HTTP requests required to render the page. This is the key to faster pages.

2: Use a Content Delivery Network

The user’s proximity to your web server has an impact on response times. Deploying your content across multiple, geographically dispersed servers will make your pages load faster from the user’s perspective.

3: Add an Expires Header

Web page designs are getting richer and richer, which means more scripts, stylesheets, images, and Flash in the page. A first-time visitor to your page may have to make several HTTP requests, but by using the Expires header you make those components cacheable. This avoids unnecessary HTTP requests on subsequent page views. Expires headers are most often used with images, but they should be used on all components including scripts, stylesheets, and Flash components.

4: Gzip Components

The time it takes to transfer an HTTP request and response across the network can be significantly reduced by decisions made by front-end engineers. It’s true that the end-user’s bandwidth speed, Internet service provider, proximity to peering exchange points, etc. are beyond the control of the development team. But there are other variables that affect response times. Compression reduces response times by reducing the size of the HTTP response.

5: Put Stylesheets at the Top

Front-end engineers that care about performance want a page to load progressively; that is, we want the browser to display whatever content it has as soon as possible. This is especially important for pages with a lot of content and for users on slower Internet connections. The importance of giving users visual feedback, such as progress indicators, has been well researched and documented. In our case the HTML page is the progress indicator! When the browser loads the page progressively the header, the navigation bar, the logo at the top, etc. all serve as visual feedback for the user who is waiting for the page. This improves the overall user experience.

6: Put Scripts at the Bottom

Rule 5 described how stylesheets near the bottom of the page prohibit progressive rendering, and how moving them to the document HEAD eliminates the problem. Scripts (external JavaScript files) pose a similar problem, but the solution is just the opposite: it’s better to move scripts from the top to as low in the page as possible. One reason is to enable progressive rendering, but another is to achieve greater download parallelization.

7: Avoid CSS Expressions

The problem with expressions is that they are evaluated more frequently than most people expect. Not only are they evaluated when the page is rendered and resized, but also when the page is scrolled and even when the user moves the mouse over the page. Adding a counter to the CSS expression allows us to keep track of when and how often a CSS expression is evaluated. Moving the mouse around the page can easily generate more than 10,000 evaluations.

8: Make JavaScript and CSS External

Using external files in the real world generally produces faster pages because the JavaScript and CSS files are cached by the browser. JavaScript and CSS that are inlined in HTML documents get downloaded every time the HTML document is requested. This reduces the number of HTTP requests that are needed, but increases the size of the HTML document. On the other hand, if the JavaScript and CSS are in external files cached by the browser, the size of the HTML document is reduced without increasing the number of HTTP requests.

9: Reduce DNS Lookups

The Domain Name System (DNS) maps hostnames to IP addresses, just as phonebooks map people’s names to their phone numbers. When you type http://www.yahoo.com into your browser, a DNS resolver contacted by the browser returns that server’s IP address. DNS has a cost. It typically takes 20-120 milliseconds for DNS to lookup the IP address for a given hostname. The browser can’t download anything from this hostname until the DNS lookup is completed.

10: Minify JavaScript

Minification is the practice of removing unnecessary characters from code to reduce its size thereby improving load times. When code is minified all comments are removed, as well as unneeded white space characters (space, newline, and tab). In the case of JavaScript, this improves response time performance because the size of the downloaded file is reduced.

11: Avoid Redirects

Redirects are accomplished using the 301 and 302 status codes. Here’s an example of the HTTP headers in a 301 response:

      HTTP/1.1 301 Moved Permanently

      Location: http://example.com/newuri

      Content-Type: text/html

The browser automatically takes the user to the URL specified in the Location field. All the information necessary for a redirect is in the headers. The body of the response is typically empty. Despite their names, neither a 301 nor a 302 response is cached in practice unless additional headers, such as Expires or Cache-Control, indicate it should be. The meta refresh tag and JavaScript are other ways to direct users to a different URL, but if you must do a redirect, the preferred technique is to use the standard 3xx HTTP status codes, primarily to ensure the back button works correctly.

12: Remove Duplicate Scripts

It hurts performance to include the same JavaScript file twice in one page. This isn’t as unusual as you might think. A review of the ten top U.S. web sites shows that two of them contain a duplicated script. Two main factors increase the odds of a script being duplicated in a single web page: team size and number of scripts. When it does happen, duplicate scripts hurt performance by creating unnecessary HTTP requests and wasted JavaScript execution.

13: Configure ETags

The problem with ETags is that they typically are constructed using attributes that make them unique to a specific server hosting a site. ETags won’t match when a browser gets the original component from one server and later tries to validate that component on a different server, a situation that is all too common on Web sites that use a cluster of servers to handle requests. By default, both Apache and IIS embed data in the ETag that dramatically reduces the odds of the validity test succeeding on web sites with multiple servers.

14: Make Ajax Cacheable

One of the cited benefits of Ajax is that it provides instantaneous feedback to the user because it requests information asynchronously from the backend web server. However, using Ajax is no guarantee that the user won’t be twiddling his thumbs waiting for those asynchronous JavaScript and XML responses to return. In many applications, whether or not the user is kept waiting depends on how Ajax is used. For example, in a web-based email client the user will be kept waiting for the results of an Ajax request to find all the email messages that match their search criteria. It’s important to remember that “asynchronous” does not imply “instantaneous”.

More details about the 14 rules can be found here: http://developer.yahoo.com/performance/rules.html

It is also possible to try all these rules on practice at http://stevesouders.com/examples/rules.php

A great web based tool for site loading time analyze is available at http://tools.pingdom.com/fpt/

How to connect to a serial port in Linux using c++?

In Linux all devices have a file in /dev directory, so the communication with these devices is very simple, just need to open necessary file, and make read and write operations upon them.

In this article is shown how to connect to serial port in Linux, using c++.

First of all we include all needed libraries, and declare all necessary variables:

#include <stdio.h>

#include <string.h>

#include <fcntl.h>

#include <errno.h>

#include <termios.h>

#include <unistd.h>

int fd1;

int fd2;

char *buff,*buffer,*bufptr;

int wr,rd,nbytes,tries;

int main()


return 0;


Next step, we connect to device through associated file and check the connection:

fd1=open(“/dev/ttyS0”, O_RDWR | O_NOCTTY | O_NDELAY);

if (fd1 == -1 )


perror(“open_port: Unable to open /dev/ttyS0 – “);




fcntl(fd1, F_SETFL,0);

printf(“Port 1 has been sucessfully opened and %d is the file description\n”,fd1);


Where, “/dev/ttyS0” is associated with COM1 port.

With following code we send to device some bits:


And for reading response from device:


printf(“Bytes sent are %d \n”,rd);

At the end, close the connection:



How to connect to a MySQL DB using c++ Linux?

C++ is a very good programming language with high-level and low-level capabilities.

Linux is a Unix-like computer operating system. It promotes community of free and open source software. Almost using those instruments it is possible to make a complex and sophisticate enterprise system.

MySQL is a multithreaded, multi-user SQL database management system. It is a popular database system, which has more than 10 million installations.

In this article is shown how to connect to a MySQL database using C++ under a Linux operating system.

First of all we include all needed libraries:

#include <sys/time.h>

#include <stdio.h>

#include <mysql.h>

Now we declare main function:

int main(char **args)


// code

return 0;


Next, we declare all necessary variables:

MYSQL_RES *result;


MYSQL *connection, mysql;

int state;

Of course if we want to use MySQL database, we must connect to it, using the next code:


connection = mysql_real_connect(&mysql,host,usr,pswd,database,0,0,0);


host – is a host name, the location of the MySQL database server, for example “localhost” or “remotehost.com”

usr – is the username

pswd – is the password

database – is the name of the MySQL database from the Host.

If something goes wrong, for example password is not right, we must know it, and display the error message:

if (connection == NULL)



return 1;


Now we make a simple query like “SELECT * FROM mytable” and check if it has no errors, where “mytable” is the name of wished table:

state = mysql_query(connection, “SELECT * FROM mytable”);

if (state !=0)



return 1;


After the successful execution of the query, we must store the results somewhere:

result = mysql_store_result(connection);

Using mysql_num_rows function, we can get number of rows from result:


Using while statement and mysql_fetch_row functions, it possible to process each row in the result set:

while ( ( row=mysql_fetch_row(result)) != NULL )


printf(” %s, %s\n”, (row[0] ? row[0] : “NULL”), (row[1] ? row[1] : “NULL” ));


At the end, we must free the memory:



IMPORTANT!!! How to make this code under Linux?

g++ test.cpp -I/usr/include/mysql /usr/lib/mysql/libmysqlclient.so


Skin color detection

Computer vision is one of many areas that wants to understand the process of human functionality and copy that process with intention to complete human life with intelligent machines. For better human – computer interaction it is necessary for the machine to see people. This can be achieved by employing face detection algorithms, which algorithm is based on skin color detection. One of the problems this and similar algorithms have to deal with is sensitivity to the illumination conditions under which the input image is captured. Hence illumination sensitivity influences face detection results. One of the aspects from which we can observe illumination influence is the choice of proper color space. Since come color space are designed to eliminate the influence of illumination when describing color of object, an idea of using such a color space for skin-color detection has been taken under consideration.

The skin color for example for fair complexion is determined with the following rules, which describe the skin cluster in the RGB color space:

R > 95 AND G > 40 AND B > 20 AND

max{R, G, B} – min{R, G, B} >15 AND

|R – G| > 15 AND

R > G AND R > B


R > 220 AND G > 210 AND B > 170 AND

|R – G| <= 15 AND

R > B AND G > B

How to teach computers to understand red color?

Why is it so important?

Our world is not so perfect! There are a lot of people who are blind, or whose sight is impaired, and hence can not locate and read attention signs. So there are some technologies what are designed to resolve that problem, such as Global Position System (GPS) and Smith-Kettlewell’s
Rehabilitation Engineering Research Center’s “Talking Signs” project. Functionality of those all solutions is to detect, locate and read general information signs, this involves segmenting signs from the background, estimating their pose, and reading them. These are all difficult problems particularly in cluttered street scenes where the illuminant color can vary greatly, where the pose and position are unknown, and where the signs can be partially obsured or in shadow.


The color can be represented in different ways, but in this case will be used RGB (Red, Green, Blue) color model. Well the conditions of understanding if some color X is red, are such:

R(X)>128 and G(X)<0.8*R(X) and B(X)<0.8*R(X)

Where R(X) – red value of X color, G(X) – green value and B(X) – blue value

Optimizing Page Load Time in PHP

There are lots of PHP optimization tricks, and all of them are good, they make loading page of Web Site faster, what is very important for end users. For example it can be done by optimizing CSS or HTML code, reducing HTTP requests, and of course optimizing the PHP code with using some function instead of other.

But, what is the solution in case our page contains many forms, and every form has some number of actions. For example:

<form action=“test.php” method=“post”>

<input type=“text” name=“txt” />

<input type=“submit” name=“form1” value=“action1” />

<input type=“submit” name=“form1” value=“action2” />


<form action=“test.php” method=“post”>

<input type=“text” name=“txt” />

<input type=“submit” name=“form2” value=“action1” />

<input type=“submit” name=“form2” value=“action2” />


The basically code of processing looks like:

if (isset($_POST[‘form1’]))


if ($_POST[‘form1’]==“action1”)




else if ($_POST[‘form1’]==“action2”)




else if ($_POST[‘form1’]==“action2”)





This is too redundant, and for a big number of forms it will be too difficult to generate this sequence of statements.

What we are doing?

The simple solution will be to use “eval” function from PHP.

First off all, we must define what forms must be processed.

// List of forms from page

$forms = array(“form1”, “form2”, “form3”);

Of no less importance is to define what method is using forms.

// Which method uses forms for sending data

$method = “POST”;

Now we must get all values, after submission action.

// Get values from submission

eval(“\$values = \$_$method;”);

Now, let’s check if some of form was submitted.

// Get the name of every form

foreach($forms as $form_name)


// check if any form was submitted

if (isset($values[$form_name]))


// Processing code



If there has been found any submitted forms then we should get the action name of this event.

// Get action name

$action_name = $values[$form_name];

Well at least after collecting all necessary information, the necessary function have to be called.

// Get function name

$function_name = $form_name.“_”.$action_name;

//Check if function exists

if (function_exists($function_name))


// call function



// quit the foreach statement,

// because only one form from page can be submitted,

// and with one action


Now just define functions of needed action processing. The function for submit action with name action2 at form with name form3, will look like:

function form3_action2()


// Code . . .


After this type of optimization, the time of loading page will dramatically improve.


Learn .htaccess in 10 minutes

Mode rewrite

A list of htaccess code snippets and examples.
Any web designer MUST know them.

Any htaccess rewrite examples should always begin with:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

This lets google crawl the page, lets me access the whole site ( without a password, and lets my client access the page WITH a password. It also allows for XHTML and CSS validation! (w3.org):

AuthName “SiteName Administration”
AuthUserFile /home/sitename.com/.htpasswd
AuthType basic
Require valid-user
Order deny,allow
Deny from all
Allow from 24\\.205\\.23\\.222
Allow from w3.org htmlhelp.com
Allow from googlebot.com
Satisfy Any

Make any file be a certain filetype (regardless of name or extension)
#Makes image.gif, blah.html, index.cgi all act as php
ForceType application/x-httpd-php:

Redirect non-https requests to https server fixing double-login problem and ensuring that htpasswd authorization can only be entered using HTTPS

SSLOptions +StrictRequire
SSLRequire %{HTTP_HOST} eq “google.com”
ErrorDocument 403 https://google.com

SEO Friendly redirects for bad/old links and moved links

For single moved file:

Redirect 301 /d/file.html http://www.htaccesselite.com/r/file.html

For multiple files like a blog/this.php?gh:

RedirectMatch 301 /blog(.*) http://www.askapache.com/$1

different domain name:

Redirect 301 / http://www.newdomain.com

Require the www:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/robots\\.txt$
RewriteCond %{HTTP_HOST} !^www\\.example\\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

Require the www without hardcoding:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/robots\\.txt$ [NC]
RewriteCond %{HTTP_HOST} !^www\\.[a-z-]+\\.[a-z]{2,6} [NC]
RewriteCond %{HTTP_HOST} ([a-z-]+\\.[a-z]{2,6})$ [NC]
RewriteRule ^/(.*)$ http://%1/$1 [R=301,L]

Require no subdomain:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/robots\\.txt$
RewriteCond %{HTTP_HOST} \\.([a-z-]+\\.[a-z]{2,6})$ [NC]
RewriteRule ^/(.*)$ http://%1/$1 [R=301,L]

Require no subdomain:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} \\.([^\\.]+\\.[^\\.0-9]+)$
RewriteCond %{REQUEST_URI} !^/robots\\.txt$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

Redirect everyone to different site except 1 IP address (useful for web-development):

ErrorDocument 403 http://www.someothersite.com
Order deny,allow
Deny from all
Allow from

Add a “en-US” language tag and “text/html; UTF-8” headers without meta tags:

AddDefaultCharset UTF-8
# Or AddType ‘text/html; charset=UTF-8’ html
DefaultLanguage en-US

Using the Files Directive:

AddDefaultCharset UTF-8
DefaultLanguage en-US

Using the FilesMatch Directive (preferred):

AddDefaultCharset UTF-8
DefaultLanguage en-US

Securing directories: Remove the ability to execute scripts:

AddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi
Options –ExecCGI
Only allow GET and PUT request methods to your server.
Options -ExecCGI -Indexes -All +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule .* – [F]

Processing All gif files to be processed through a cgi script:

Action image/gif /cgi-bin/filter.cgi

Process request/file depending on the request method:

Script PUT /cgi-bin/upload.cgi

Force Files to download, not be displayed in browser:

AddType application/octet-stream .avi
AddType application/octet-stream .mpg

Dramatically Speed up your site by implementing Caching!:

Header set Cache-Control “max-age=2592000”
Header set Cache-Control “max-age=604800”
Header set Cache-Control “max-age=43200”

Prevent Files image/file hotlinking and bandwidth stealing:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\\.)?askapache.com/.*$ [NC]
RewriteRule \\.(gif|jpg|swf|flv|png)$ http://www.askapache.com/evil-hotlinker.gif [R=302,L]


ErrorDocument 404 /favicon.ico
ErrorDocument 403 https://secure.htaccesselite.com
ErrorDocument 404 /cgi-bin/error.php
ErrorDocument 400 /cgi-bin/error.php
ErrorDocument 401 /cgi-bin/error.php
ErrorDocument 403 /cgi-bin/error.php
ErrorDocument 405 /cgi-bin/error.php
ErrorDocument 406 /cgi-bin/error.php
ErrorDocument 409 /cgi-bin/error.php
ErrorDocument 413 /cgi-bin/error.php
ErrorDocument 414 /cgi-bin/error.php
ErrorDocument 500 /cgi-bin/error.php
ErrorDocument 501 /cgi-bin/error.php

Authentication Magic

Require password for 1 file:

AuthName “Prompt”
AuthType Basic
AuthUserFile /home/askapache.com/.htpasswd
Require valid-user

Protect multiple files:

AuthName “Development”
AuthUserFile /.htpasswd
AuthType basic
Require valid-user

Example uses of the Allow Directive:

# A (partial) domain-name
Allow from

# Full IP address
Allow from

# More than 1 full IP address
Allow from

# Partial IP addresses
# first 1 to 3 bytes of IP, for subnet restriction.
Allow from 10.1
Allow from 10 172.20 192.168.2

# network/netmask pair
Allow from

# network/nnn CIDR specification
Allow from

# IPv6 addresses and subnets
Allow from 2001:db8::a00:20ff:fea7:ccea
Allow from 2001:db8::a00:20ff:fea7:ccea/10

Using visitor dependent environment variables:

SetEnvIf User-Agent ^KnockKnock/2\\.0 let_me_in
Order Deny,Allow
Deny from all
Allow from env=let_me_in

block access to files during certain hours of the day:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# If the hour is 16 (4 PM) Then deny all access
RewriteCond %{TIME_HOUR} ^16$
RewriteRule ^.*$ – [F,L]