Posts Tagged Apache
15Oct/091
How to speed up loading of your website
Is your website slow? Do you even realize that it could load faster?Every webmaster tries to make web sites faster, so people will get better user experience. But there are few tricks that not everyone is aware of.
How to improve loading speed?
- cache static content (pictures, javascripts, css files)
- remove ETag from pictures
- compress content
First thing is to cache static content so that it can be loaded from visitor's computer and not from server on every visit. Caching will also decrease bandwidth. It's good to set expire tag for at least a year.
Every picture sent by a server include ETag by which browser can check if a picture from a server match picture from browser's cache. This is not needed on static content, which will never change (for example, uploaded pictures).
And don't forget about compressing content. Loading will be faster because a browser needs to download a compressed file which is smaller and it will save a little bit of bandwidth. Compression is good for static and also dynamic content (.htm/.html/.css/.js/.php).
Example .htaccess file:
Header unset ETag
FileETag None
<FilesMatch "^.*\\.(ico|flv|jpg|jpeg|png|gif|js|css)$">
Header unset Last-Modified
Header set Expires "Fri, 21 Dec 2012 00:00:00 GMT"
Header set Cache-Control "public, no-transform"
</FilesMatch>
<FilesMatch "\\.(js|css)$">
SetOutputFilter DEFLATE
</FilesMatch>
You need to put this file in a root directory of your website. Be careful, file doesn't have any name, only extension "htaccess". If you're using nice/SEO URLs then there is a good chance of you having this file already in a root directory. Only thing you need to do is to open existing file and copy this example code on the bottom of the file. It will only work with Apache HTTPD Server (most popular http server).
Compressing dynamic PHP file
To compress PHP file you need to add following code on the top of the file:
<?php
ob_start("ob_gzhandler");
?>
Or a bit safer method, that will turn off GZIP compression if visitor's web browser doesn't support it:
<?php
if(!ob_start("ob_gzhandler")) ob_start();
?>
You can check site's performance with YSlow add-on for Firefox's developer plugin named Firebug.
Download: http://developer.yahoo.com/yslow/
27Sep/090
Setting Charset on an Apache Server
How to demand from an Apache Server to serve pages with a specific encoding?You will ask this question sooner or later if you are making web sites. Probably sooner if you are dealing with non-ASCII characters.
There are a lot of charsets in the world, from ones that support most languages to ones that are very specific. In web development business UTF-8 is a standard, specially on non-english web sites. It's not that big of a problem on HTML sites, but it can be a hell of a mess if you have server with first encoding, PHP with second encoding and MySQL with completely different encoding.
In this post, I will show you how to tell Apache to use specific charset (in our examples I'll use UTF-8).
The easiest way to set encoding is by following meta tag in HTML:
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
But this is not always enough. So to be sure you need to specify charset in the Apache configuration file or in .htaccess.
You can use one of the following methods:
<FilesMatch "\\.(htm|html|css|js|php)$">
ForceType 'text/html; charset=UTF-8'
</FilesMatch>
or
AddType 'text/html; charset=UTF-8' html
or
AddCharset UTF-8 .html
or
AddDefaultCharset UTF-8
I recommend last option because I had no problems what so ever with it so far. It always works fine. (Other should too.)