📜 ⬆️ ⬇️

Easy setup .htaccess for production

Sometimes, when sites are hosted on a shared-hosting or work only with Apache, you need to make the maximum optimization of the server and the site, respectively. The article provides several settings that will allow your site to work better.



1. Encoding


Always specify the encoding. By default, it is better to choose utf-8.
AddDefaultCharset UTF-8 

')

2. Redirect to one domain


For SEO optimization, it is desirable that the site be placed on only one domain. If you have several domains referring to one site, we redirect to the main domain. It is desirable that it starts with www.

Let's do it using code 301

 <IfModule mod_rewrite.c> 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] </IfModule> 


3. Caching static resources


Long page load takes away loading static resources. Most of them never change. But the browser at each request checks whether the resource on the server has changed. In response, the browser receives the code 304 - the resource has not changed, and takes the resource from the cache. That is, even if the resource is in the cache, the browser makes a request. In general, this results in an additional load on the server and in slowing down the page parsing.

Get rid of it. To do this, set the time for checking the update of the resource for several years in advance, and disable the check for ETag

 <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$"> Header set Cache-Control "public" Header set Expires "Fri, 21 Dec 2012 20:00:00 GMT" #Until the end of the world FileETag None </FilesMatch> 


4. Compress text files


These files can be transferred to the browser in compressed form. We indicate the necessary 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 


findings


These settings will suit most sites that work with Apache, optimizing server performance and page loading speed.

Source: https://habr.com/ru/post/145298/


All Articles