Simple .htaccess settings for production
Sometimes, we use shared hosting to host our sites or we use only Apache server. How to do the best optimization of server work and site load time? This article describes how to do it in easy way.
1. Encoding
It is always necessary to determine encoding. Better to use utf-8 by default.
AddDefaultCharset UTF-8
2. Use redirect to one domain
It is desirable For SEO that site will be accessible only from one domain name. If you have several aliases of sites, it is need to set up redirects to main domain. I recommend start this domain with www. Let us make it using 301 code. See 301(wiki).
Options +Followsymlinks RewriteEngine On RewriteCond %{HTTP_HOST} ^example\.com [OR] RewriteCond %{HTTP_HOST} ^example2\.com [OR] RewriteCond %{HTTP_HOST} ^www\.example2\.com RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
3. Cache of static resources
A lot of page load time takes loading of static resources (css, js, images). Most of them are never changed. But browser checks server about changes during each request. In response browser gets code 304 - Not Modified, and takes these resources from local cache. So, even browser has some resource in cache, it makes request to server. In general it cause additional server load and slowdown of parsing page. How to escape this? Lets set up time of expire for resources for a some years forward and disable checking for ETag. What is Etag? See ETag(wiki)
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$"> Header set Cache-Control "public" Header set Expires "Fri, 21 Dec 2112 20:00:00 GMT" #Until the end of the world FileETag None
4. Compressing of text files
Text files like css, js files we can send to server in compressed state. Set up needed directives for this:
AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/x-javascript
Conclusions
These settings go very well for most of sites, which is working under Apache web server, help to optimize server work and speed up page load time.













