You are here

function _cdn_seo_should_redirect in CDN 7.2

Determines whether a redirect should be performed for the given path for SEO considerations (prevent duplicate HTML content on the CDN), and if so, the URL to which the requesting User Agent should be redirected.

Parameters

$path: The path for which to determine the redirect URL.

Return value

FALSE if no redirect should occur, or the URL to redirect to.

1 call to _cdn_seo_should_redirect()
CDNSEOTestCase::testSEO in tests/cdn.test
1 string reference to '_cdn_seo_should_redirect'
cdn.constants.inc in ./cdn.constants.inc
Constants used by the CDN module.

File

./cdn.module, line 986

Code

function _cdn_seo_should_redirect($path) {
  if (variable_get(CDN_SEO_REDIRECT_VARIABLE, CDN_SEO_REDIRECT_DEFAULT)) {

    // If the path ends in an extension that is not in the list of forbidden
    // extensions, then return FALSE to indicate that no redirect should occur.
    // The rationale is: menu_get_item() doesn't allow us to detect whether a
    // page callback will generate a file; hence we (ab)use this heuristic.
    // An added benefit is that we don't need the menu system to be loaded,
    // meaning that we can do all of this during hook_boot(), meaning we can use
    // this same code for cached pages, which we need to support anyway.
    // @todo: improve Drupal core so that contrib modules can know whether
    // certain menu callbacks generate files or not.
    $forbidden_extensions = variable_get(CDN_SEO_FORBIDDEN_EXTENSIONS_VARIABLE, CDN_SEO_FORBIDDEN_EXTENSIONS_DEFAULT);
    $extension = drupal_strtolower(pathinfo($path, PATHINFO_EXTENSION));
    if (!empty($extension) && !in_array($extension, explode("\n", $forbidden_extensions))) {
      return FALSE;
    }

    // Use case-insensitive substring matching to match the current User-Agent
    // to the list of CDN user agents.
    if (isset($_SERVER['HTTP_USER_AGENT'])) {
      $ua = drupal_strtolower($_SERVER['HTTP_USER_AGENT']);
      $cdn_user_agents = explode("\n", drupal_strtolower(variable_get(CDN_SEO_USER_AGENTS_VARIABLE, CDN_SEO_USER_AGENTS_DEFAULT)));
      foreach ($cdn_user_agents as $cdn_ua) {
        if (strstr($ua, trim($cdn_ua))) {
          return url($path, array(
            'absolute' => TRUE,
          ));
        }
      }
    }
  }
  return FALSE;
}