Updating your Media & Documents CDN URL

Follow

By default, your Media and Documents in Agility are served over a globally distributed CDN using either cdn.agilitycms.com (for customers with their source media in US) or cdn-ca.agilitycms.com (for customers with their source media in Canada).

Due to SEO implications, Agility recommends that each customer have their own unique domain alias for the the CDN. 

You may either provide a custom domain name to use (i.e. cdn.customer-domain.com) or Agility may provide one using the *.azureedge.net domain name. Agility also recommends that if you provide a custom domain, that you also provide (or purchase) a valid SSL certificate so that CDN resources may be loaded over HTTPS.

The first step to getting started is to contact support@agilitycms.com and inform them of your intent to utilize a custom CDN URL. 

Once your CDN profile is setup by our support team, you will then need to update your CDN URL within your website application. Agility recommends adding a replacement filter to your website code that will search and replace all instances of the default CDN URL with your custom CDN URL. This means that even if editors within the CMS accidentally use the default CDN URL, your website application will be smart enough to replace these instances on the fly as the HTML is outputted by the server.

Below, is an example of a Replacement Filter Module that may be installed in an MVC application:

ReplacementFilterModule.cs 

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;

namespace ReplacementFilter
{
    public class ReplacementFilterModule : IHttpModule
    {
        public String ModuleName
        {
            get { return "ReplacementFilterModule"; }
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }


        void context_BeginRequest(object sender, EventArgs e)
        {

            HttpContext context = HttpContext.Current;

            string fileName = context.Request.FilePath;
            string ext = string.Format("{0}", System.IO.Path.GetExtension(fileName)).ToLower();



            if (
                //context.Request.IsSecureConnection
                !string.Equals(ext, ".jpg")
                && !string.Equals(ext, ".jpeg")
                && !string.Equals(ext, ".gif")
                && !string.Equals(ext, ".png")
                && !string.Equals(ext, ".css")
                && !string.Equals(ext, ".js")

                )
            {
                //only do the replacements if we are on a secure connnection...
                context.Response.Filter = new ReplacementStream(context.Response.Filter);
            }
        }


        public void Dispose()
        {

        }
    }

    public class ReplacementStream : MemoryStream
    {
        private Stream stream;
        private StreamWriter streamWriter;

        public ReplacementStream(Stream stm)
        {
            stream = stm;
            streamWriter = new StreamWriter(stream, System.Text.Encoding.UTF8);
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            string contentType = HttpContext.Current.Response.ContentType;

            if (contentType.IndexOf("text", StringComparison.CurrentCultureIgnoreCase) != -1 ||
                contentType.IndexOf("json", StringComparison.CurrentCultureIgnoreCase) != -1)
            {
                string find = "http://cdn.agilitycms.com/"; //the default CDN URL
                string find2 = "http://origin.agilitycms.com/"; //the origin URL where your source media is - this URL should never be used in production as it circumvents the CDN
                string replace = "https://cdn.custom-domain.com/"; //only https if you also have an SSL certificate added to the CDN profile
                
                string html = System.Text.Encoding.UTF8.GetString(buffer);
                html = html.Replace(find, replace); //replace the text
                html = html.Replace(find2, replace); //replace the text
     
                streamWriter.Write(html.ToCharArray(), 0, html.ToCharArray().Length);
                streamWriter.Flush();
            }
            else
            {
                stream.Write(buffer, 0, buffer.Length);
                stream.Flush();
            }
        }


    }
}

Register the Replacement Module in web.config:

...
   <system.webServer>    
<modules runAllManagedModulesForAllRequests="true">
      <add name="ReplacementFilter.ReplacementFilterModule" type="ReplacementFilter.ReplacementFilterModule" />
    </modules>
</system.webServer>         
...

If you have any questions or concerns, please contact support@agilitycms.com and we'll be happy to help!

0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.