You are here

function boost_admin_generate_htaccess in Boost 6

Generate htaccess code.

http://www.askapache.com/htaccess/mod_rewrite-variables-cheatsheet.html

Return value

string htaccess code

3 calls to boost_admin_generate_htaccess()
boost_admin_boost_performance_page in ./boost.admin.inc
Form builder; Displays Boost's configuration page.
boost_admin_htaccess_page in ./boost.admin.inc
Form builder; Displays Boost's htaccess generation page.
boost_update_htaccess in ./boost.admin.inc
Helper function to update htaccess Inserts or removes the autogenerated boost rules

File

./boost.admin.inc, line 1325
All the code for the Boost module's administrative interface.

Code

function boost_admin_generate_htaccess() {
  global $base_path;
  $server_name = variable_get('boost_server_name_http_host', '%{HTTP_HOST}');
  $document_root = variable_get('boost_document_root', '%{DOCUMENT_ROOT}');
  $drupal_subdir = rtrim($base_path, '/');

  // Various dir's
  $cache_dir = BOOST_ROOT_CACHE_DIR;
  $gzip_dir = !(BOOST_GZIP_DIR == '') ? '/' . BOOST_GZIP_DIR . '/' : '/';
  $normal_dir = !(BOOST_NORMAL_DIR == '') ? '/' . BOOST_NORMAL_DIR . '/' : '/';
  $permanent_dir_gzip = !(BOOST_PERM_GZ_DIR == '') ? '/' . BOOST_PERM_GZ_DIR . '/' : '/';
  $permanent_dir = !(BOOST_PERM_NORMAL_DIR == '') ? '/' . BOOST_PERM_NORMAL_DIR . '/' : '/';

  // with a \ slash
  $html = str_replace('.', '\\.', BOOST_FILE_EXTENSION);
  $xml = str_replace('.', '\\.', BOOST_XML_EXTENSION);
  $css = str_replace('.', '\\.', BOOST_CSS_EXTENSION);
  $js = str_replace('.', '\\.', BOOST_JS_EXTENSION);
  $json = str_replace('.', '\\.', BOOST_JSON_EXTENSION);
  $gz = str_replace('.', '\\.', BOOST_GZIP_EXTENSION);
  $char = BOOST_CHAR;
  $permanent_char = BOOST_PERM_CHAR;
  $server_ip = str_replace('.', '\\.', $_SERVER['SERVER_ADDR']);

  // Generate the rules
  $string = "  ### BOOST START ###\n";
  if (BOOST_AGGRESSIVE_GZIP) {
    $string .= "\n";
    $string .= "  # Gzip Cookie Test\n";
    $string .= "  RewriteRule ^(.*)boost-gzip-cookie-test\\.html {$cache_dir}{$permanent_dir_gzip}" . "boost-gzip-cookie-test\\.html\\.gz [L,T=text/html,E=no-gzip:1]\n";
  }
  if (BOOST_CACHE_CSS || BOOST_CACHE_JS) {
    if (BOOST_GZIP) {
      $skip = BOOST_CACHE_CSS && BOOST_CACHE_JS ? 2 : 1;
      $string .= "\n";
      $string .= "  # GZIP - Cached css & js files\n";
      $string .= BOOST_AGGRESSIVE_GZIP ? "  RewriteCond %{HTTP_COOKIE} !(boost-gzip)\n" : '';
      $string .= "  RewriteCond %{HTTP:Accept-encoding} !gzip\n";
      $string .= "  RewriteRule .* - [S={$skip}]\n";
      if (BOOST_CACHE_CSS) {
        $string .= "  RewriteCond {$document_root}{$base_path}{$cache_dir}{$permanent_dir_gzip}{$server_name}%{REQUEST_URI}{$permanent_char}{$css}{$gz} -s\n";
        $string .= "  RewriteRule .* {$cache_dir}{$permanent_dir_gzip}{$server_name}%{REQUEST_URI}{$permanent_char}{$css}{$gz} [L,QSA,T=text/css,E=no-gzip:1]\n";
      }
      if (BOOST_CACHE_JS) {
        $string .= "  RewriteCond {$document_root}{$base_path}{$cache_dir}{$permanent_dir_gzip}{$server_name}%{REQUEST_URI}{$permanent_char}{$js}{$gz} -s\n";
        $string .= "  RewriteRule .* {$cache_dir}{$permanent_dir_gzip}{$server_name}%{REQUEST_URI}{$permanent_char}{$js}{$gz} [L,QSA,T=text/javascript,E=no-gzip:1]\n";
      }
    }
    $string .= "\n";
    $string .= "  # NORMAL - Cached css & js files\n";
    if (BOOST_CACHE_CSS) {
      $string .= "  RewriteCond {$document_root}{$base_path}{$cache_dir}{$permanent_dir}{$server_name}%{REQUEST_URI}{$permanent_char}{$css} -s\n";
      $string .= "  RewriteRule .* {$cache_dir}{$permanent_dir}{$server_name}%{REQUEST_URI}{$permanent_char}{$css} [L,QSA,T=text/css]\n";
    }
    if (BOOST_CACHE_JS) {
      $string .= "  RewriteCond {$document_root}{$base_path}{$cache_dir}{$permanent_dir}{$server_name}%{REQUEST_URI}{$permanent_char}{$js} -s\n";
      $string .= "  RewriteRule .* {$cache_dir}{$permanent_dir}{$server_name}%{REQUEST_URI}{$permanent_char}{$js} [L,QSA,T=text/javascript]\n";
    }
  }
  if (BOOST_CACHE_HTML || BOOST_CACHE_XML || BOOST_CACHE_JSON) {
    $skip = (int) BOOST_CACHE_HTML + (int) BOOST_CACHE_XML + (int) BOOST_CACHE_JSON;
    $skip = BOOST_GZIP ? $skip * 2 + 1 : $skip;
    $string .= "\n";
    $string .= "  # Caching for anonymous users\n";
    $string .= "  # Skip boost IF not get request OR uri has wrong dir OR cookie is set OR request came from this server" . (variable_get('boost_ssl_bypass', TRUE) ? " OR https request" : "") . "\n";
    $string .= "  RewriteCond %{REQUEST_METHOD} !^(GET|HEAD)\$ [OR]\n";
    $string .= "  RewriteCond %{REQUEST_URI} (^{$base_path}(admin|{$cache_dir}|misc|modules|sites|system|openid|themes|node/add|comment/reply))|(/(edit|user|user/(login|password|register))\$) [OR]\n";
    $string .= BOOST_LOOPBACK_BYPASS && BOOST_OVERWRITE_FILE ? "  RewriteCond %{REMOTE_ADDR} ^{$server_ip}\$ [OR]\n" : '';
    if (variable_get('boost_apache_rfc2616', FALSE)) {
      $string .= "  RewriteCond %{HTTP:Pragma} no-cache [OR]\n";
      $string .= "  RewriteCond %{HTTP:Cache-Control} no-cache [OR]\n";
    }
    if (variable_get('boost_ssl_bypass', TRUE)) {
      $string .= "  RewriteCond %{HTTPS} on [OR]\n";
    }
    $string .= "  RewriteCond %{HTTP_COOKIE} DRUPAL_UID\n";
    $string .= "  RewriteRule .* - [S={$skip}]\n";
    if (BOOST_GZIP) {
      $skip = (int) BOOST_CACHE_HTML + (int) BOOST_CACHE_XML + (int) BOOST_CACHE_JSON;
      $string .= "\n";
      $string .= "  # GZIP\n";
      $string .= BOOST_AGGRESSIVE_GZIP ? "  RewriteCond %{HTTP_COOKIE} !(boost-gzip)\n" : '';
      $string .= "  RewriteCond %{HTTP:Accept-encoding} !gzip\n";
      $string .= "  RewriteRule .* - [S={$skip}]\n";
      if (BOOST_CACHE_HTML) {
        $string .= "  RewriteCond {$document_root}{$base_path}{$cache_dir}{$gzip_dir}{$server_name}%{REQUEST_URI}{$char}%{QUERY_STRING}{$html}{$gz} -s\n";
        $string .= "  RewriteRule .* {$cache_dir}{$gzip_dir}{$server_name}%{REQUEST_URI}{$char}%{QUERY_STRING}{$html}{$gz} [L,T=text/html,E=no-gzip:1]\n";
      }
      if (BOOST_CACHE_XML) {
        $string .= "  RewriteCond {$document_root}{$base_path}{$cache_dir}{$gzip_dir}{$server_name}%{REQUEST_URI}{$char}%{QUERY_STRING}{$xml}{$gz} -s\n";
        $string .= "  RewriteRule .* {$cache_dir}{$gzip_dir}{$server_name}%{REQUEST_URI}{$char}%{QUERY_STRING}{$xml}{$gz} [L,T=text/xml,E=no-gzip:1]\n";
      }
      if (BOOST_CACHE_JSON) {
        $string .= "  RewriteCond {$document_root}{$base_path}{$cache_dir}{$gzip_dir}{$server_name}%{REQUEST_URI}{$char}%{QUERY_STRING}{$json}{$gz} -s\n";
        $string .= "  RewriteRule .* {$cache_dir}{$gzip_dir}{$server_name}%{REQUEST_URI}{$char}%{QUERY_STRING}{$json}{$gz} [L,T=text/javascript,E=no-gzip:1]\n";
      }
    }
    $string .= "\n";
    $string .= "  # NORMAL\n";
    if (BOOST_CACHE_HTML) {
      $string .= "  RewriteCond {$document_root}{$base_path}{$cache_dir}{$normal_dir}{$server_name}%{REQUEST_URI}{$char}%{QUERY_STRING}{$html} -s\n";
      $string .= "  RewriteRule .* {$cache_dir}{$normal_dir}{$server_name}%{REQUEST_URI}{$char}%{QUERY_STRING}{$html} [L,T=text/html]\n";
    }
    if (BOOST_CACHE_XML) {
      $string .= "  RewriteCond {$document_root}{$base_path}{$cache_dir}{$normal_dir}{$server_name}%{REQUEST_URI}{$char}%{QUERY_STRING}{$xml} -s\n";
      $string .= "  RewriteRule .* {$cache_dir}{$normal_dir}{$server_name}%{REQUEST_URI}{$char}%{QUERY_STRING}{$xml} [L,T=text/xml]\n";
    }
    if (BOOST_CACHE_JSON) {
      $string .= "  RewriteCond {$document_root}{$base_path}{$cache_dir}{$normal_dir}{$server_name}%{REQUEST_URI}{$char}%{QUERY_STRING}{$json} -s\n";
      $string .= "  RewriteRule .* {$cache_dir}{$normal_dir}{$server_name}%{REQUEST_URI}{$char}%{QUERY_STRING}{$json} [L,T=text/javascript]\n";
    }
  }
  $string .= "\n";
  $string .= "  ### BOOST END ###\n";
  return $string;
}