How to Force Mura CMS to Use Primary Domains in SEO-Friendly Way

Mura CMS allows you to have multiple URLs assigned to each particular site. For example, you could have http://domain.com be your 'primary' domain and http://www.domain.com and http://www.anotherdomain.com set up as 'domain aliases' so that when you navigate to any one of these domains, Mura will serve up the desired site. This is pretty cool actually and there are a number of use cases out there on why you would want that to happen. However, if you want the 'primary' domain to be the one and only one used for SEO purposes and such, then you'll need to make just a couple of minor tweaks to make that happen. The best part is, I'll show you just how easy this is!

The first thing you'll need to do is copy the following method into your local contentRenderer.cfc. It's located at /{siteid}/includes/contentRenderer.cfc (or if you want this to be 'theme-specific' then edit the one located at /{siteid}/includes/themes/{themeName}/contentRenderer.cfc).

<cffunction name="doEnforceDomain" access="public" output="false" returntype="any" hint="By default, I will redirect users in an SEO-friendly way to your primary domain ... you may optionally override this by giving me an alternate 'primary' domain to enforce.">
    <cfargument name="domain" required="false" default="#$.siteConfig('domain')#" />
    <cfscript>
        var local = StructNew();
        local.doRedirect = false;
        local.servername = getPageContext().getRequest().getServerName();
        if ( local.servername neq arguments.domain ) {
            local.doRedirect = true;
            local.servername = arguments.domain;
            local.baseurl = getPageContext().getRequest().getScheme() & '://' & local.servername;
            // if not using a standard port, we should prolly include it
            local.port = getPageContext().getRequest().getServerPort();
            if ( local.port neq '80' and local.port neq '443' ) {
                local.baseurl = local.baseurl & ':' & local.port;
            };
            // uri returns '/index.cfm/path/to/page/'
            local.uri = getPageContext().getRequest().getRequestURI();
            local.urlstr = local.baseurl & local.uri;
            // append query string, if any
            if ( len(trim(getPageContext().getRequest().getQueryString())) ) {
                local.urlstr = local.urlstr & '?' & getPageContext().getRequest().getQueryString();
            };
        };
    
</cfscript>        
    <cfif local.doRedirect>
        <cflocation url="#local.urlstr#" addtoken="false" statuscode="301" />
        <cfelse>
        <cfreturn />
    </cfif>
</cffunction>

The last thing you'll need is an itsy-bitsy, teeny-weeny line of code that can actually be dropped in a number of different places. More on that in a second. However, if you want your entire site to always use the primary domain, then drop the following line of code into your local eventHandler.cfc in the onRenderStart() method. This is located in the same locations as the contentRenderer.cfc.

<cffunction name="onRenderStart">
    <cfargument name="$" />
    <cfset $.getContentRenderer().doEnforceDomain() />
</cffunction>

Remember how I mentioned you could drop this in a number of different places? Well, let me explain. Let's say you have an area of the site that uses a particular template and you want ONLY that area of the site to use a specific domain...for example 'http://docs.domain.com' for a documents area. Well instead of using the eventHandler.cfc route, you could just drop this at the top of the template and then anytime someone visits this area, they'll get re-routed properly and in a SEO-friendly manner. Isn't that sweet?

<cfscript>
    $.getContentRenderer().doEnforceDomain('docs.domain.com');
</cfscript>

As always, I hope this helps!

Cheers!

Comments

This is really awesome Steve, and something that I have been wishing that the BR guys would roll into Mura's base code.
# Posted By Dave Long | 4/26/11 11:15 AM
@Dave,

Well, considering I''m one of those 'Blue River' guys...I guess I should get on the ball then, eh?

I'll see if there's a clean way to implement something like this into the core. Maybe make the method available, and even have a radio option in the Site Settings for each site or something. Hmm.

Cheers!
# Posted By Steve Withington | 4/26/11 9:04 PM
Thanks Steve. Very useful.
# Posted By Jeff | 6/23/11 9:34 AM
Steve, thanks for this great piece of code.
I made one modification because the site I am using it on does not use "index.cfm" in the URLs.

I added:
<cfset local.urlstr = replacenocase(local.urlstr,"/index.cfm","")>

right after <cfif local.doRedirect> in the contentRenderer.cfc

that keeps it from adding in the "index.cfm" in the 301 redirect
# Posted By Jeff Pierson | 1/10/12 7:43 PM

© 2024, Stephen J. Withington, Jr.  |  Hosted by Hostek.com

Creative Commons License   |   This work is licensed under a Creative Commons Attribution 3.0 Unported License.