HOW TO PROXY AND ACCELERATE WORDPRESS USING EDGERULES

Lets say that for years you had your company’s blog under a subdomain like blog.example.com. While this might work great for site management and updates, when it comes to SEO your business is really missing out. Google views your main site and your blog as two separate sites, preventing your root domain from getting the SEO juice your blog subdomain is providing.

The obvious solution is to move your blog to a sub-directory, like www.example.com/blog. Unfortunately, this isn’t always possible and even if it is it might not be the ideal solution. Your server might only support static sites, or you may be taking advantage of some other aspect of completely separate installs, e.g. dedicated WordPress hosting. In this case, what are your options?

Well you can always completely rebuild your infrastructure to support PHP and then move your WordPress installation to the new domain. Or you can use EdgeRules and simplify the process as much as possible.

EdgeRules is a product by MaxCDN that essentially allows you to build custom Nginx rules and deploy them on the Edge servers of our CDN. Using them we can build an Nginx config that will proxy the blog from the previous domain and make it available under a subdirectory.

Step 1: Accelerate your website.

Since your website is static you can use MaxCDN to cache the full thing on our CDN without any issues and make it super fast with great uptime. To accomplish this, create a pull zone and point it to the server where your website is hosted. In order to point MaxCDN to your origin, you must create a special subdomain such as max.domain.com. Make sure that you’ve enabled both your DNS and server to work with the subdomain. If you’re using NGINX, the only thing you’ll need to do is simply add the new domain to your existing vhost:

server_name www.company.com company.com max.company.com;

Next, in your newly created pull zone you will need to add www.company.com as your custom domain.

blog-proxy-domain-settings

Now all you have to do is to switch your DNS for www.company.com to point to your pull zone’s CDN link. Visit our support site for a more in-depth explanation.

Congrats! Now your main website is much faster. If you enable “Use stale” in your settings in case your server goes down, MaxCDN will keep your website online. At this point, you have the option to install an SSL certificate on your pull zone to support HTTPS connections to your website. No configuration on the origin is required. Again, our support site can provide detailed information.

Step 2: Prepare your WordPress blog

Now since the blog itself is going to stay on the same domain and server we need to make sure nothing breaks when proxying to a new domain and directory.

Static Blog Content

Make sure all content is served using relative paths. This means that your js/css/images need to be served either from a CDN domain or from a relative location. So instead of blog.company.com/image.jpg it should be just image.jpg. By doing this the browser will request that image from whatever domain we are on, in this case it will be www.company.com/blog/image.jpg. In most cases you wont have to do anything as it will be already relative, hardcoding a domain in the source code is a very bad practice.

Links

Make sure all links are relative. All internal links like blog posts and pages will need to be relative as well. Otherwise the user will be hitting the old domain which is not good.
The easiest solution is to install this plugin https://wordpress.org/plugins/relative-url/

If you prefer to minimize your use of plugins, simply use the following code to add filters to rewrite all on-page links generated using WordPress. You can rewrite permalinks, tags, categories, author links and more.

function make_href_root_relative($input) {
     return preg_replace('!http(s)?://' . $_SERVER['SERVER_NAME'] . '/!', 'https://www.maxcdn.com/blog/', $input);
}
add_filter( 'the_permalink', 'make_href_root_relative' );
add_filter( 'tag_link', 'make_href_root_relative' );
add_filter( 'category_link', 'make_href_root_relative' );
add_filter( 'author_link', 'make_href_root_relative' );
add_filter( 'get_comments_link', 'make_href_root_relative' );
add_theme_support('root-relative-urls');

Meta Tags and Social Media

Once your links are relative, there’s one step left – making sure that all of the links in the page head are accurate. If you’re like us, you’re most likely using a WordPress plugin to help with your SEO and social media tags. You’ll need to check and see what meta tags are being generated automatically, and either disable them or use a filter to alter the tags.

The first step for me was taking care of all the social media tags. I disabled them all in the Yoast plugin, and manually added the tags to the page head. Use WordPress hooks for the title and excerpt, and handcode all company information. Make sure you add the link at the appropriate domain – we want to minimize all redirects and instances of the old URL. Here’s how we added the OpenGraph URL:

<?php echo 'https://www.maxcdn.com/blog' . $_SERVER['REQUEST_URI']; ?>

Updating the canonical URL is incredibly important, and something you must make sure is taken care of. The canonical URL needs to point to the new domain as well. On our blog, the Yoast SEO plugin was providing our canonical URL. Luckily a plugin filter is available which we used to rewrite the canonical URL:

function rewrite_canonical_url($postURL) {
        $newURL = str_replace('https://www.maxcdn.com/blog/', 'https://www.maxcdn.com/blog/', $postURL);
        return $newURL;
}
add_filter( 'wpseo_canonical', 'rewrite_canonical_url' );

Step 3: EdgeRules

Now its time to proxy the blog. Go to the EdgeRules tab of your pull zone that serves your main website. Create a new rule and use the following settings:

blog-proxy-edgerule

The Proxy Pass needs to point to the IP of the server that hosts the blog (make sure you replace 0.0.0.0 with the correct IP of your server). A domain name won’t work in this case. And to get the actual blog using the IP we need to send to the server the hostname that we need, in this case blog.company.com.

We also need to make sure the users will see an up-to-date website. For this reason we are saying the caching time or TTL to 10 minutes for everything and 1 month for all static content. Depending on the popularity of your website and frequency of updates you may want to tweak these numbers.

Save the rule and now go to www.company.com/blog. You should now see your blog.

Step 4: Redirects

Last but not least, you’ll need to make sure that the original subdomain (blog.domain.com) will now redirect to domain.com/blog with a 301 redirect. This allows you to avoid any issues with duplicate content. Make sure that all redirects carry through the entire URL, so that requests for specific blog posts are redirected to the same blog post at the new location.

The post How to Proxy and Accelerate WordPress Using EdgeRules appeared first on MaxCDN Blog.

Leave a Reply

Your email address will not be published. Required fields are marked *