This page has been robot translated, sorry for typos if any. Original content here.

Several ways to configure the .htaccess file for site optimization

On this topic:


Несколько способов настройки файла .htaccess для оптимизации сайта

Surely, many have heard about this file. The .htaccess file is the heart of the server on which the site is hosted. It is he who is responsible for exactly how the site will react to the actions that will be carried out by visitors. I was picking on this file a little.

y, and somehow it became interesting, and I was picking at .htaccess. Therefore, today about the settings. Htaccess, with which you can optimize the site a little. In general, everything in order.

Those snippets, which I'll give below, can be inserted into a .htaccess file. This file is located in the root folder of the Apache web server.

Attention! Always need to insure. If you twist, twist, something that you do not really know, always back up.

Cross-domain font integration for Firefox

 <filesmatch "\. (ttf | otf | eot | woff) $">
 <ifmodule mod_headers.c>
	 Header set Access-Control-Allow-Origin "http://yourdomain.com"
 </ ifmodule>
 </ filesmatch>

There is such a small kosyachok in Firefox. This browser does not allow you to integrate fonts from other sites. But this problem is solved with the help of .htaccess

Running PHP inside javascript files

 AddType application / x-httpd-php .js
 AddHandler x-httpd-php5 .js
 
 <filesmatch "\. (js | php) $">
 SetHandler application / x-httpd-php
 </ filesmatch>	

Often happens that inside .js files it is necessary to push php. For example, in order to be able to receive some data from the database. The code below will solve this problem.

We are keeping a log of PHP errors

 # display no errs to user
 php_flag display_startup_errors off
 php_flag display_errors off
 php_flag html_errors off
 #log to file
 php_flag log_errors on
 php_value error_log /location/to/php_error.log

Often the php shoal can be replaced directly on the site. The problem is solved. You can make sure that these errors are written to the log file. Then ordinary visitors will not see these glitches, but they will be perfectly visible to you.

Setting up a site for working with html5-video

 RewriteCond% {REQUEST_FILENAME}! -F
 RewriteCond% {REQUEST_FILENAME}! -D
 RewriteCond% {REQUEST_URI}! = / Favicon.ico
 AddType video / ogg .ogv
 AddType video / ogg .ogg
 AddType video / mp4 .mp4
 AddType video / webm .webm
 AddType application / x-shockwave-flash swf

Those functions that were with the old format of the hypertext markup language were imperfect. This all could be understood when html5 appeared. Among its many functions there is an excellent opportunity to integrate video without Flash. The only thing that needs to be done correctly is to configure the server so that it works under html5. Here's the solution.

Redirecting different newsletters into one format

 <ifmodule mod_alias.c>
 RedirectMatch 301 / feed / (atom | rdf | rss | rss2) /? $ Http://example.com/feed/
 RedirectMatch 301 / comments / feed / (atom | rdf | rss | rss2) /? $ Http://example.com/comments/feed/
 </ ifmodule>

Remember about RSS or Atom feed formats? There are no questions, all this is needed. But just uncomfortable when they live apart. It is much more convenient that all formats be coded in one. Of course, in RSS. With the help of this snippet, this issue can be solved. By the way, this is especially true when it comes to WordPress.

Let's fight spammers in WordPress

 <ifmodule mod_rewrite.c>
 RewriteEngine On
 RewriteCond% {REQUEST_METHOD} POST
 RewriteCond% {REQUEST_URI} .wp-comments-post \ .php *
 RewriteCond% {HTTP_REFERER}!. * Yourdomainname. * [OR]
 RewriteCond% {HTTP_USER_AGENT} ^ $
 RewriteRule (. *) ^ Http: //% {REMOTE_ADDR} / $ [R = 301, L]
 </ ifmodule>

No, well, for anyone now, I'll think that there is Akismet. Nafiga these extra settings. Yes, Akismet works well, but the same problem can be solved more practical, through .htaccess. The problem is solved by prohibiting access to bots. It's just forbidden access to wp-comments-post.php.

Using the .htaccess cache to speed up the site

 # 1 YEAR
 <filesmatch "\. (ico | pdf | flv) $">
 Header set Cache-Control "max-age = 29030400, public"
 </ filesmatch>
 # 1 WEEK
 <filesmatch "\. (jpg | jpeg | png | gif | swf) $">
 Header set Cache-Control "max-age = 604800, public"
 </ filesmatch>
 # 2 DAYS
 <filesmatch "\. (xml | txt | css | js) $">
 Header set Cache-Control "max-age = 172800, proxy-revalidate"
 </ filesmatch>
 # 1 MIN
 <filesmatch "\. (html | htm | php) $">
 Header set Cache-Control "max-age = 60, private, proxy-revalidate"
 </ filesmatch>	

There are a lot of useful things on the list today, but, in my opinion, this is the most useful of them. By caching .htaccess you can well overclock your site. On my site this option showed a good result.

Mobile device redirection

 RewriteEngine On
 RewriteCond% {REQUEST_URI}! ^ / M /.*$
 RewriteCond% {HTTP_ACCEPT} "text / vnd.wap.wml | application / vnd.wap.xhtml + xml" [NC, OR]
 RewriteCond% {HTTP_USER_AGENT} "acs | alav | alca | amoi | audi | aste | avan | benq | bird | blac | blaz | brew | cell | cldc | cmd-" [NC, OR]
 RewriteCond% {HTTP_USER_AGENT} "dang | doco | eric | hipt | inno | ipaq | java | jigs | kddi | keji | leno | lg-c | lg-d | lg-g | lge-" [NC, OR]
 RewriteCond% {HTTP_USER_AGENT} "maui | maxo | midp | mits | mmef | mobi | mot- | moto | mwbp | nec- | newt | noki | opwv" [NC, OR]
 RewriteCond% {HTTP_USER_AGENT} "palm | pana | pant | pdxg | phil | play | pluc | port | prox | qtek | qwap | sage | sams | sany" [NC, OR]
 RewriteCond% {HTTP_USER_AGENT} "sch- | sec- | send | seri | sgh- | shar | sie- | siem | smal | smar | sony | sph- | symb | ​​t-mo" [NC, OR]
 RewriteCond% {HTTP_USER_AGENT} "teli | tim- | tosh | tsm- | upg1 | upsi | vk-v | voda | w3cs | wap- | wapa | wapi" [NC, OR]
 RewriteCond% {HTTP_USER_AGENT} "wapp | wapr | webc | winw | winw | xda | xda-" [NC, OR]
 RewriteCond% {HTTP_USER_AGENT} "up.browser | up.link | windowssce | iemobile | mini | mmp" [NC, OR]
 RewriteCond% {HTTP_USER_AGENT} "symbian | midp | wap | phone | pocket | mobile | pda | psp" [NC]
 # ------------- The line below excludes the iPad
 RewriteCond% {HTTP_USER_AGENT}! ^. * IPad. * $
 # -------------
 RewriteCond% {HTTP_USER_AGENT}! Macintosh [NC] # * SEE NOTE BELOW
 RewriteRule ^ (. *) $ / M / [L, R = 302]	

If you are trying to create a mobile version of the site, then why should users, who come to the usual one, climb there. They need to be redirected to the mobile version.