You are here

function boost_admin_htaccess_generate_htaccess in Boost 7

Generate htaccess code.

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

Return value

string htaccess code

1 call to boost_admin_htaccess_generate_htaccess()
boost_admin_htaccess_generation in ./boost.admin.htaccess.inc
Form builder; Configure boost settings.

File

./boost.admin.htaccess.inc, line 205
Admin page callbacks for the boost module.

Code

function boost_admin_htaccess_generate_htaccess() {
  global $base_path;
  $server_name = variable_get('boost_server_name_http_host', BOOST_SERVER_NAME_HTTP_HOST);
  $document_root = variable_get('boost_document_root', BOOST_DOCUMENT_ROOT);
  $drupal_subdir = rtrim($base_path, '/');

  // Various dir's.
  $cache_dir = variable_get('boost_root_cache_dir', BOOST_ROOT_CACHE_DIR);
  $normal_dir = variable_get('boost_normal_dir', BOOST_NORMAL_DIR);
  $char = variable_get('boost_char', BOOST_CHAR);

  // Go through every storage type getting the extesion and if it supports gzip.
  $enabled_file_extensions = array();
  $types = boost_get_storage_types();
  foreach ($types as $title => $content_types) {
    foreach ($content_types as $type => $values) {
      if ($values['enabled']) {
        $enabled_file_extensions[$values['extension']]['gzip'] = $values['gzip'];
        if (empty($enabled_file_extensions[$values['extension']]['content_type'])) {
          $enabled_file_extensions[$values['extension']]['content_type'] = $type;
        }
      }
    }
  }
  $output = array(
    'gzip' => '',
    'normal' => '',
  );
  $gzip_count = 0;
  $normal_count = 0;
  foreach ($enabled_file_extensions as $extension => $values) {
    $type = $values['content_type'];
    $output['normal'] .= "  RewriteCond {$document_root}{$base_path}{$cache_dir}/%{ENV:boostpath}/{$server_name}%{REQUEST_URI}{$char}%{QUERY_STRING}\\.{$extension} -s\n";
    $output['normal'] .= "  RewriteRule .* {$cache_dir}/%{ENV:boostpath}/{$server_name}%{REQUEST_URI}{$char}%{QUERY_STRING}\\.{$extension} [L,T={$type}]\n";
    $normal_count++;
  }
  $skip = !empty($gzip_count) ? $normal_count + $gzip_count + 1 : $normal_count + 1;

  // Generate the rules.
  $string = "  ### BOOST START ###\n";
  if (!empty($output)) {
    $string .= "\n";
    $string .= "  # Allow for alt paths to be set via htaccess rules; allows for cached variants (future mobile support)\n";
    $string .= "  RewriteRule .* - [E=boostpath:{$normal_dir}]\n";
    $string .= "\n";
    $string .= "#  # Apache 2.4 bug workaround\n";
    $string .= "#  # Enables Search from home page https://drupal.org/node/2078595#comment-8724321\n";
    $string .= "#  RewriteCond %{REQUEST_METHOD} ^(POST)\$\n";
    $string .= "#  RewriteCond %{REQUEST_URI} {$base_path}\n";
    $string .= "#  RewriteRule .* {$base_path} [S=" . ($skip + 1) . "]\n";
    $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', BOOST_SSL_BYPASS) ? " 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";
    if (variable_get('boost_ssl_bypass', BOOST_SSL_BYPASS)) {
      $string .= "  RewriteCond %{HTTPS} on [OR]\n";
    }
    $string .= "  RewriteCond %{HTTP_COOKIE} " . variable_get('boost_cookie', BOOST_COOKIE) . " [OR]\n";
    $string .= "  RewriteCond %{ENV:REDIRECT_STATUS} 200\n";
    $string .= "  RewriteRule .* - [S={$skip}]\n";
    $string .= "\n";
    $string .= "#  # Apache 2.4 bug workaround\n";
    $string .= "#  # Enables caching of index/ home page\n";
    $string .= "#  RewriteCond %{REQUEST_URI} ^" . "{$base_path}" . "index\\.php\$\n";
    $string .= "#  RewriteCond {$document_root}{$base_path}{$cache_dir}/%{ENV:boostpath}/%{HTTP_HOST}" . $base_path . "\\" . $char . "%{QUERY_STRING}\\.html -s\n";
    $string .= "#  RewriteRule .* {$cache_dir}/%{ENV:boostpath}/{$server_name}{$base_path}" . "\\" . $char . "%{QUERY_STRING}\\.html [L,T=text/html]";
    $string .= "\n";
    $string .= "\n";
    $string .= "  # NORMAL\n";
    $string .= $output['normal'];
  }
  $string .= "\n";
  $string .= "  ### BOOST END ###\n";
  return $string;
}