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

Several ways to customize the .htaccess file to optimize the site

On this topic:


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

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

u, but 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, about everything in order.

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

Attention! Always need to insure. If something is twisting, muddle, something that you do not know much, then 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 bong at Firefox. This browser does not allow to integrate fonts from other sites. But this problem is solved with .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>	

It often happens that inside the .js files you need to push php. For example, in order to be able to get some data from the database. The code below will solve this problem.

We are logging 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 php shoals can be replaced directly on the site. The problem is solved. You can make these errors recorded in a log file. Then ordinary visitors will not see these glitches, but they will be perfectly visible to you.

Site setup 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. Everyone could understand this when html5 appeared. Among its many features there is a great opportunity to integrate video without Flash. The only thing that will need to properly configure the server so that it works under html5. Here is the solution.

Redirection of various mailings in 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 feeds? No questions, all this is necessary. But just uncomfortable when they live apart. It is much more convenient that all formats are brought into one. Of course, in RSS. With this snippet, this issue can be resolved. By the way, this is especially true when it comes to WordPress.

We will fight back 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 anybody now, I’ll think about what Akismet is. What for these extra settings. Yes, Akismet helps a lot, but the same problem can be solved more practically through .htaccess. The problem is solved by denying access to bots. Access to wp-comments-post.php is simply prohibited.

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>	

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

Mobile 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 puffed over the creation of a mobile version of the site, then why so that users who come to the usual, climbed there. They need to be redirected to the mobile version.