Simple CDN (cookie free domain).

Public

Sometimes we just want simple cookie free domain for our files, this simple snippet serves all files (css, js, images, etc.) via domain or subdomain which point to the same drupal root.
You can add a variable to settings.php: $conf['mymodule_cdn'] = 'http://cdn.mydomain.com';
Code borrowed from cdn module - http://drupal.org/project/cdn

Get raw version
php
  1. /**
  2.  * Implements hook_file_url_alter().
  3.  */
  4. function MYMODULE_file_url_alter(&$original_uri) {
  5.  
  6. $scheme = file_uri_scheme($original_uri);
  7.  
  8. // If the current URI is an absolute or protocol-relative URI, return
  9. // immediately.
  10. if (($scheme && ($scheme == 'http' || $scheme == 'https'))
  11. || drupal_substr($original_uri, 0, 2) == '//'
  12. )
  13. {
  14. return;
  15. }
  16. elseif ($scheme) {
  17. // Only alter URLs for local stream wrappers. If a file is served
  18. // remotely, it doesn't make sense to serve it from a CDN.
  19. $local_schemes = array_keys(file_get_stream_wrappers(STREAM_WRAPPERS_LOCAL));
  20. if (!in_array($scheme, $local_schemes)) {
  21. return;
  22. }
  23.  
  24. // Attempt to get an external URL using the appropriate wrapper.
  25. if ($wrapper = file_stream_wrapper_get_instance_by_uri($original_uri)) {
  26. $uri = str_replace($GLOBALS['base_url'] . '/', '', $wrapper->getExternalUrl());
  27.  
  28. $original_uri = variable_get('MYMODULE_cdn', 'http://CDN.MYDOMAIN.COM') . '/' . $uri;
  29. return;
  30. }
  31. else {
  32. return;
  33. }
  34. }
  35. else {
  36. $original_uri = variable_get('MYMODULE_cdn', 'http://CDN.MYDOMAIN.COM') . '/' . $original_uri;
  37. }
  38.  
  39. }