optimization.png
  • RedDot CMS Blog
  • 22.03.2017
  • EN

rdb: RedDot & 301 Redirects: How to manage 301’s within your RedDot CMS projects

One of the biggest risks when relaunching a new version of a site is losing search rankings that may have taken a considerable amount of time and effort to earn. The use of 301 Redirects will help you to maintain current search rankings for an existing site, particularly if the site structure has gone under the knife! Existing pages may have been relocated or consolidated to a new area of the site – or may have been removed altogether.
 

What exactly is a 301 redirect?

A 301 redirect instructs search engines that a URL’s name has changed perminantely. All previous information held about this page, including all it’s incoming link value and most important search engine position is to be transfered to a new page name.

Unfortunately 301 redirects cannot be managed straight out of box within RedDot CMS – since they need to be handled on the live web server. Fortunately, through the use of some basic server code integrated into your site templates, 301 redirects can be managed quite easily by content editors and administrators.
 

301 Redirects within RedDot CMS

For most RedDot CMS projects I have implemented in the past, I have found it easiest to manage all 301 redirects for a site within a custom 404 error page (every site should have a custom 404 error page anyway!). Not only does the page enable Users to manage the content for the 404 error page (try and be as creative as you can in this department!!!), it also doubles as an area to handle the required 301 redirects for the site. If a page is requested that does not exist or is no longer available, the web server will serve up the 404 error page which will enable the code for the 301 redirects to kick in if a match is found against the initial page request.
 

How does it work?

The key concept here is that we are creating a searchable table or list of 301 redirects that is matched against the initial page request. If a match is found, the user is redirected to the new page otherwise the 404 error page is displayed instead.

In this example I’ve used some C#.NET code to handle the 301 redirects, however the concept can quite easily be translated to any other language.
 

Here’s how to do it:

1. Create a new foundation template within your project specifically to manage a custom 404 error page:

 ASP.NET 
<%@ Page Language="C#" AutoEventWireup="true" %>
<%
String pageRequested = "";
Dictionary redirects = new Dictionary();

pageRequested = Request["aspxerrorpath"].ToString().ToLower();

// remove querystring variables (if any)
if (pageRequested.IndexOf("?") > -1 )
{
pageRequested = pageRequested.Substring(0, pageRequested.IndexOf("?"));
}
%>

<h1>404 Error Page</h1>

<!IoRangeRedDotMode>
<div class="RedDot"><!IoRedDotOpenPage> [edit page]</div>
<!/IoRangeRedDotMode>
<h1>404 Error Page</h1>
<!IoRangeRedDotEditOnly>
<div class="RedDot"><!IoRedDot_txt_404errormessage> [default error message]</div>
<!/IoRangeRedDotEditOnly>
<%txt_404errormessage%>

<!IoRangeRedDotEditOnly>
<div class="RedDot"><!IoRedDot_con_301redirects> [maintain 301 redirects]</div>
<!/IoRangeRedDotEditOnly>
<%con_301redirects%>

<%
// perform redirect if match is found
if (redirects.ContainsKey(pageRequested))
{
Response.Clear();
Response.StatusCode = 301;
Response.AddHeader("Location", redirects[pageRequested]);
Response.End();
}
%>


Heres a break down of whats going on:

 ASP.NET 
pageRequested = Request["aspxerrorpath"].ToString().ToLower();

Obtain URL of page that does not exist

 ASP.NET 
if (pageRequested.IndexOf("?") > -1 )
{
pageRequested = pageRequested.Substring(0, pageRequested.IndexOf("?"));
}
%>

Remove any querystring variables from the page since we wont be matching against those

 Element 
<%txt_404errormessage%>

Custom error message to be displayed on the 404 error page if no 301 redirect matches were found.

 Element 
<%con_301redirects%>

Container where all of the 301 redirects are managed within the project.

 ASP.NET 
if (redirects.ContainsKey(pageRequested))
{
Response.Clear();
Response.StatusCode = 301;
Response.AddHeader("Location", redirects[pageRequested]);
Response.End();
}

Find any matches against the page request and redirect the user to the new page
 

2. Create a new template which will enable users to create each of the individual 301 redirects. Each page created from this template will be connected up within the ‘con_301redirects’ container on the 404 error page

 ASP.NET / RenderTags 
<reddot:cms>
    <if>
        <query valuea="Context:CurrentRenderMode" operator="==" valueb="Int:2">
            <htmltext>
                <%
                ' add new redirect
                if ("<%stf_oldurl%>" != "")
                {
                    redirects.Add("<%stf_oldurl%>".ToLower(), "<%anc_newurl%>");
                }
                %>
            </htmltext>
        </query>
    </if>
</reddot:cms>
<!IoRangeRedDotMode>
<div class="RedDot"><!IoRedDotOpenPage> [edit 301 redirect]
<!IoRangeRedDotEditOnly><span class="details"><span class="details-title">TITLE:</span> <%hdl_pagetitle%> | <span class="details-title">ID:</span> <%info_pageid%></span><!/IoRangeRedDotEditOnly></div>
<!IoRangeRedDotEditOnly>
<div class="RedDot"><!IoRedDot_stf_oldurl> [old URL]</div>
old URL: <%stf_oldurl%>
<!IoRangeRedDotEditOnly>
<div class="RedDot"><!IoRedDot_anc_newurl> [new URL]</div>
<!/IoRangeRedDotEditOnly>
new URL: <!IoRangeDynLink><a href="<%anc_newurl%>"><%hdl_pagetitle%></a>
<!/IoRangeDynLink>
<!/IoRangeRedDotMode>

Within RedDot, users enter in the ‘old’ and ‘new’ urls for the 301 redirect using the corresponding RedDot placeholders above.
When the page is published, the 301 redirect will be used to build up searchable table (in this example I’m using the C# Dictionary object) against which a match can be found based on the page request.
 

Extending things further

This is just a basic example of how you can manage 301 redirects within your RedDot CMS projects. I’m currently working on providing a feature to enable CMS users manage 301’s within individual site pages (rather than the 404 page), which in turn populates an XML document that contains all 301 redirects for the site. Watch this space…!

 

Social Media