Monday, September 19, 2011

Permanent Redirects in ActiveCampus CMS

I recently received a request on how to handle Permanent Redirects (301 Redirects) in Datatel's Active Campus... So without further ado, here is my solution to that problem.

First thing I did was create a new Template, in my case titled Template404.aspx. Be sure to include any .NET Controls specific to your implementation...

<%@ OutputCache NoStore="true" Duration="1" VaryByParam="none" %>
<%@ Page Language="C#" AutoEventWireup="true" Inherits="ActiveCampus.Websites.Controls.CMSPage" StyleSheets="/Website Resources/css/SiteStyle.css:all|/Website Resources/css/SiteStylePrint.css:print" Async="true" %>
<%@ Register Src="/Templates/controls/404control.ascx" tagName="control404" TagPrefix="control404" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head id="Head1" runat="server">
    <title>Central Wyoming College - Page Not Found Error</title>
    <script language="javascript" src="/Media/Website%20Resources/scripts/mootools.js" type="text/javascript"></script>
    <script language="javascript" src="/Media/Website%20Resources/scripts/AC-Core.js" type="text/javascript"></script>
    <script language="javascript" src="/Media/Website%20Resources/scripts/AC-LightBox.js" type="text/javascript"></script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<!--[if lte IE 6]>
    <link rel="stylesheet" type="text/css" href="/Media/Website%20Resources/css/SiteStyleIE6.css" />
    <![endif]-->
<link rel="icon" type="image/vnd.microsoft.icon" href="/Media/Website Resources/Favicon.ico" />
</head>
<body id="body" runat="server">
<div id="pageCt" class="search">
    <form id="form1" action="/" runat="server">
        <div id="pageCtInner">
            <div id="pageCtBG">
                <div id="zoneCt">
                    <div id="gridCt">                                            
                        <div id="zone2">
                            <div id="centerContent">
                                <control404:control404 id="control404" runat="server" />
                            </div>
                        </div>
                        <div id="zone1">
                
                        </div>
                        <div class="spacer"> </div>    
                    </div>
                    <div class="spacer"> </div>
                </div>
                <div class="spacer"> </div>
            </div>
            <div class="spacer"> </div>
            <div class="spacer"> </div>
        </div>
        <div class="spacer"> </div>
    </form>
    </div>
</body>
</html>
Next, I needed to create the .NET control that actually handles all of my redirects. Now mind, you my method is to have all redirects in a central location. This is also useful for those short hand URL's that you want to go somewhere deep into the site. Like perhaps, www.cwc.edu/library will actually take you to www.cwc.edu/resources/library. This way our library, has a short link they can advertise on their materials. Also, much easier for users to remember the shorter version, then trying to remember to add in resources. So for my example, I'm going to handle some Library Page redirections we used. To add additional, pages, you simply add additional case statements. Additionally, the last part of this script, also takes care of the case when somebody enters cwc.edu (I then want to push them automatically to the www.).

<%@ Language=VBScript %>

<%
' Permanent redirection
Dim str = Request.RawUrl.Replace("/404error.aspx?404;http://www.cwc.edu:80","")
str = str.Replace("/404error.aspx?404;http://cwc.edu:80","")
str = str.tolower
If Request.QueryString("aspxerrorpath") <> String.Empty Then
   str = Request.QueryString("aspxerrorpath").tolower
End If
'Handle Academic Program URLs
If str.indexOf("/academics/programs-of-study/") >=0 or str.indexOf("/Academics/Programs-of-Study/") >=0 Then
      Response.Status = "301 Moved Permanently"
      Response.AddHeader ("Location", "http://www.cwc.edu/academics/programs/")
else
Select Case str.tolower
   Case "/resouces/library/testingcenter/testcenterrec.aspx"
      Response.Status = "301 Moved Permanently"
      Response.AddHeader("Location","http://www.cwc.edu/resources/Library/testingcenter/testcenterrec.html")
   Case "/resources/library/testingcenter/fees.aspx"
      Response.Status = "301 Moved Permanently"
      Response.AddHeader("Location","http://www.cwc.edu/resources/Library/testingcenter/Fees.htm")
   Case "/resources/library/testingcenter/clep.aspx"
      Response.Status = "301 Moved Permanently"
      Response.AddHeader("Location","http://www.cwc.edu/resources/Library/testingcenter/Clep.htm")
   Case "/resources/library/testingcenter/default.aspx"
      Response.Status = "301 Moved Permanently"
      Response.AddHeader("Location","http://www.cwc.edu/resources/Library/testingcenter/default.htm")
   Case "/resources/library/librarydistance.aspx"
      Response.Status = "301 Moved Permanently"
      Response.AddHeader("Location","http://www.cwc.edu/resources/Library/librarydistance.htm")



   Case Else
      Response.write ("<p>Oops... This page can't be found. Please visit the <a href='http://www.cwc.edu/azindex.aspx'>Site Map</a> for a listing of pages.<br />Error Page: " & str & "<br />Also try search at the top of the website to find the page you are looking for.")
Dim strRootURL = Request.URL.ToString.Replace("http://cwc.edu/Cache/Templates/Template404.aspx?=404;","")
'Response.write("<p>URL: " & strRootURL)
if strRootURL.startswith("http://cwc.edu") Then
strRootURL = strRootURL.Replace("http://cwc.edu","http://www.cwc.edu")
Response.Redirect(strRootURL)
End If
End Select
End If
Response.End
%>
Next you'll add this template to the Website Workflow. You do that by going to Website, Right clicking the top "Website" folder and selecting Workflow Properties. Click the Template Groups Tab then select your template bundle. Now click the Templates tab in the bottom section. Goto the bottom, and click the add button to add your template. Click Ok, and exit the workflow.

Now you should be able to create a page with this template. It is not necessary to enter the template designer. Just make sure the page is published. Make sure the page loads up in your browser by pointing directly to it.

This next part, will cause a short pause and delay, so I recommend that you do it during non-peak hours. Also, create a backup of your web.config file before making any changes.

On the public website instance, open the web.config file. And search for <customerrors. Assuming you have not modified it at all, you can change it to:

<customErrors mode="On" defaultRedirect="ErrorPages/ErrorPage.aspx" />
Modify the defaultRedirect location to be the location of your error. In our case, we want users to always just think it is a page not found error, even if there is a programming error in a form. But you can also modify it to handle just 404 error types.

That should do it.

Hopefully this helps you out down the road!

Paul

No comments: