Several ways to configure 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 on which the site is hosted. It is he who is responsible for exactly how the site will respond to the actions that will be carried out by visitors. I was picking on this file a little.

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 something twists and turns, then what you do not really know, 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 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, phashes 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 in the old format of the hypertext markup language were imperfect. It was all 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's 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 to access 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.