WordPress站点启用CDN,其实就是切换图片、CSS等元素的URL到CDN域名,这要通过插件实现。官方扩展市场里很多相关插件,但都过于庞大,尤其多数都是集成在大型插件里面的小功能。 CDN Enabler 应该是最轻量级的件了,它本身也是一家CDN服务商的配套插件。插件本身的说明如下
The WordPress CDN Enabler plugin is a content delivery network (CDN) integration plugin that rewrites URLs, such as those serving CSS, JavaScript, and images, to be served by a CDN. It supports including URLs in the rewrite by file extension and excluding URLs in the rewrite by string. When a KeyCDN account is connected the CDN cache can be purged from WordPress. This simple, lightweight plugin makes integrating a WordPress website with a CDN a breeze.
不过,本插件有个缺陷:如果WordPress站点在域名下的子目录里,原版插件就不支持了,它只允许WordPress在根目录下。
所以,这里做了个简单修改,允许CDN在一个子目录中;使用的是是 2.0.8 版本,截止写作时的最新版本。完整插件代码如下
cdn-enabler_v2.0.8-patched_cdn-subdir
事实上,在查阅原版插件代码时,似乎其早期版本是支持子目录的。
另外,如果使用自定义CDN服务,那么该插件就不支持向CDN发送通知的功能(比强制让缓存失效等)。