远方的海
远方的海,WordPress,免费
2014 Aug 06 16 : 59

WordPress 非插件七牛CDN全站加速

您当前的位置: 首页 > WordPress > 正文
欢迎光临远方的海!您可能还对 WordPress建站免费资源最新IT动态 感兴趣,点击开启 悦读 模式。CTRL+D 快捷收藏本文,我们期待您再次光临!

Wordpress 非插件七牛CDN全站加速

如果你还没有七牛的账户,或者对七牛的基本操作不是很了解,先看这篇文章:点击前往,如果你对七牛已经很熟悉就不用看了~

非插件实现wordpress的七牛CDN全站加速,水煮鱼有一个七牛的插件,也有人有super cache来用七牛CDN。现在给出一个不要插件的方法,插件能少一个就少一个吧!

把以下代码加入functions.php中:位置一定要放对,在<?php 后面一行就好。第三行和第四行要根据你的网站域名和对应的七牛域名更改!白名单的意思是不对这些文件进行CDN加速。如果你的网站喜欢折腾,建议先把css文件放入白名单里面。

注意:代码第三行和第四行的域名要根据自己的改。

 
//Code from http://seavia.com/
//七牛CDN
define('FocusCDNHost','http://www.seavia.com');//wordpress网站网址
define('FocusCDNRemote','http://seavia.qiniudn.com');//cdn域名
define('FocusCDNIncludes','wp-content,wp-includes');//设置加速目录
define('FocusCDNExcludes','.php|.xml|.html|');//设置文件白名单
define('FocusCDNRelative',");//Check this if you want to have links like <wp-content/abc.png> rewritten – i.e. without your blog's domain as prefix.
function do_cdnrewrite_ob_start() {
$rewriter = new FocusCDNRewriteWordpress();
$rewriter->register_as_output_buffer();
}
add_action('template_redirect', 'do_cdnrewrite_ob_start');
class FocusCDNRewriteWordpress extends FocusCDNRewrite
{
function __construct() {
$excl_tmp = FocusCDNExcludes;
$excludes = array_map('trim', explode('|', $excl_tmp));
parent::__construct(
FocusCDNHost,
FocusCDNRemote,
FocusCDNIncludes,
$excludes,
!!FocusCDNRelative
);}
public function register_as_output_buffer() {
if ($this->blog_url != FocusCDNRemote) {
ob_start(array(&$this, 'rewrite'));
}}}
class FocusCDNRewrite {
var $blog_url    = null;
var $cdn_url     = null;
var $include_dirs   = null;
var $excludes    = array();
var $rootrelative   = false;
function __construct($blog_url, $cdn_url, $include_dirs, array $excludes, $root_relative) {
$this->blog_url   = $blog_url;
$this->cdn_url    = $cdn_url;
$this->include_dirs  = $include_dirs;
$this->excludes   = $excludes;
$this->rootrelative  = $root_relative;
}
protected function exclude_single(&$match) {
foreach ($this->excludes as $badword) {
if (stristr($match, $badword) != false) {
return true;}}
return false;}
protected function rewrite_single(&$match) {
if ($this->exclude_single($match[0])) {
return $match[0];
} else {
if (!$this->rootrelative || strstr($match[0], $this->blog_url)) {
return str_replace($this->blog_url, $this->cdn_url, $match[0]);
} else {
return $this->cdn_url . $match[0];
}}}
  protected function include_dirs_to_pattern() {
$input = explode(',', $this->include_dirs);
if ($this->include_dirs == " || count($input) < 1) { return 'wp-content|wp-includes'; } else { return implode('|', array_map('quotemeta', array_map('trim', $input))); }}   public function rewrite(&$content) { $dirs = $this->include_dirs_to_pattern();
$regex = '#(?<=[("'])'; $regex .= $this->rootrelative
? ('(?:'.quotemeta($this->blog_url).')?')
: quotemeta($this->blog_url);
$regex .= '/(?:((?:'.$dirs.')[^"')]+)|([^/"']+.[^/"')]+))(?=["')])#';
return preg_replace_callback($regex, array(&$this, 'rewrite_single'), $content);
}  }

以上。

好文!分享给朋友,或者点个赞吧~

文章信息

分类:WordPress

您可能也会喜欢

发表回复

Post Comment


  1. M-DD1 :

    Parse error: syntax error, unexpected ‘{‘ in /home/u773788496/public_html/wp-content/themes/Parallax-One/functions.php on line 59
    主页变成这样了
    怎么弄啊。。。。。。。。。。。。

    2015-12-26
  2. 泥图网 :

    还是直接用的插件。。

    2014-8-10
    • 远方的海水神 :

      这个超赞。插件影响速度。

      2014-8-10
  3. 云落 :

    Internal Server Error

    The server encountered an internal error or misconfiguration and was unable to complete your request.

    Please contact the server administrator at webmaster@googlo.me to inform them of the time this error occurred, and the actions you performed just before this error.

    More information about this error may be available in the server error log.

    Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
    Apache/2 Server at googlo.me Port 80

    2014-8-6
    • Sven水神 :

      500没有关系。改回去就好了。复制代码要注意大小写。放在end WordPress前面。

      2014-8-7
      • Sven水神 :

        你再试试看,我又重新试过了可以的。

        2014-8-7
  4. 云落 :

    能不能具体些,多写点,然后,刚刚看了那个修改.htacess的文章,然后现在500中。。。。。。

    2014-8-6
    • Sven水神 :

      放在end wordpress那行前面。而且是Linux的方法。

      2014-8-7
    • Sven水神 :

      这个直接放在主题functions.php里面就好了。又把文章改详细了!

      2014-8-7