URL rewriting is a URL redirection technology implemented in web server software that maps an incoming HTTP requests to a different URL. For example, a request for the page
through the use of URL rewriting.
Some spider and bot software agents refuse to follow URLs that terminate in URL variables as they crawl or index a web site. Although the major search engines do not experience this problem, the above example would be a solution to allow that kind of software agent to properly crawl the web site.
Another application of URL rewriting is to permit more human-readable URLs. Through a database lookup to the content management system's folder structure URL rewriting could map
This article will explore the first case, URL rewriting to remove URL variables, as implemented through an ASP.NET HTTP module.
Two modifications are necessary to use URL rewriting with the Ektron CMS. The web server must be modified to perform the actual URL rewrites and the CMS system must be modified to create content block quicklinks that use the new URL format.
In a site with ASP.NET templates a custom HTTP module can be used to accomplish the rewrite. Creating and using a custom HTTP module involves coding and compiling the HTTP module class as a .dll, placing the .dll in the web application's bin/ subdirectory, and registering the HTTP module in the web.config file.
The HTTP module must implement the System.Web.IHttpModule interface. A minimal implementation in C# is as follows:
using System;
using System.Web;
using System.Text.RegularExpressions;
namespace ekCMSURLReWrite
{
public abstract class BaseModuleRewriter : IHttpModule
{
public virtual void Init(HttpApplication app)
{
app.BeginRequest += new
EventHandler(this.BaseModuleRewriter_BeginRequest);
}
public virtual void Dispose() {}
protected virtual void BaseModuleRewriter_BeginRequest(
object sender, EventArgs e)
{
HttpApplication app = (HttpApplication) sender;
Rewrite(app.Request.Path, app);
}
protected abstract void Rewrite(string requestedPath,
HttpApplication app);
}
public class ekReWriter : BaseModuleRewriter
{
protected override void Rewrite(string requestedPath,
System.Web.HttpApplication app)
{
app.Context.RewritePath(Regex.Replace(app.Context.Request.Path, @"(_id(?<id>[0-9]*).aspx)\z", @".aspx?id=${id}", RegexOptions.IgnoreCase));
}
}
}
The last method "Rewrite" is of interest to us. It uses a regular expression object to match URLs ending with the pattern "_idn.aspx" where n represents any series of digits. That pattern is replaced with ".aspx?id=n".
The above class can either be compiled with Visual Studio .NET or the command line C# compiler that is included with the .NET framework. The files included with this tutorial are from Visual Studio .NET. For an example of using the C# command line compiler, refer to the tutorial
HOWTO: CMS300 Web Services Syndication Without Visual Studio
.
The above code was taken primarily from Scott Mitchell's URL Rewriting in ASP.NET article on MSDN:
http://msdn.microsoft.com/netframework/default.aspx?pull=/library/en-us/dnaspp/html/urlrewriting.asp
After compiling the class as a .dll it must be placed into the bin/ subdirectory of the web site. It may be necessary to create this subdirectory if it has not already been created by a development tool. The directory for the web site must also be marked as an application in the IIS control panel as in CMS setup of Web Services (Ektron CMS Developer Reference Manual -> Web Services -> Setup Instructions). These steps will make the HTTP module class available to ASP.NET code in all subdirectories of the application.
Finally the HTTP module must be registered with the ASP.NET parsing engine with an "add" entry in the httpModules section of the web.config file. The following complete web.config file may be used if you don't have an existing one.
<configuration>
<system.web>
<customErrors mode="Off"/>
<httpModules>
<add type="ekCMSURLReWrite.ekReWriter, ekCMSURLReWrite"
name="ekReWriter" />
</httpModules>
</system.web>
</configuration>
After all of the above steps have been taken URLs will be rewritten as in the first example near the beginning of this article.
For sites using another scripting language, another implementation method available for URL rewriting is the creation of an ISAPI extension for the Windows IIS web server. Many third-party products are offered that implement URL rewriting through ISAPI and a web search will return many articles describing the necessary code. An example is the following page at IISFAQ.com:
http://www.iisfaq.com/Default.aspx?tabid=3194
Next the quicklinks that the CMS creates must be altered to use the new format. Setting the quicklink when a new content block is created is done internally within the COM objects in CMS300 but we can add some ASP code to the CMS300 Work Area that will immediately update the quicklink.
The page that presents the Add Content form is cms300scripts/edit.asp. Open it in your development environment and search for the string "AddNewContent". In CMS300 version 4.5.0.0 this search returns the following COM object call:
ContentID = ekContObj.AddNewContentv2_0(AppConfStr, currentUserID, cCont, Site, ErrorString)
This call creates the database entries for the new content block. We will add our code to update the quicklink immediately after it:
DIM readconn, rs, sqlselect, sqlupdate1, x, writeconn1, ra, reSearchFor, Matches, newqlink
set readconn=Server.CreateObject("ADODB.Connection")
set writeconn1=Server.CreateObject("ADODB.Connection")
readconn.Open DSN, DBusername, DBpassword
set rs=Server.CreateObject("ADODB.recordset")
sqlselect="SELECT lib_id, filename from library WHERE content_id = " & ContentID
rs.Open sqlselect, readconn
if (rs.RecordCount > 0) then
rs.MoveFirst
Set reSearchFor = New RegExp
reSearchFor.Pattern = ".aspx\?id=([0-9]*)"
reSearchFor.IgnoreCase = True
reSearchFor.Global = True
newqlink=reSearchFor.Replace(rs.fields("filename"),"_id$1.aspx")
sqlupdate1 = "UPDATE library SET filename = '" & newqlink & "' WHERE (lib_id = " & rs.fields("lib_id") & ")"
writeconn1.Open DSN, DBusername, DBpassword
writeconn1.Execute sqlupdate1, ra
writeconn1.close
end if
rs.close
readconn.close
After the new content block is created the above code will query the CMS database to retrieve the quicklink for the new content block. If a quicklink is found a regular expression reformats the URL. The database is updated with the new quicklink.
The example edit.asp page included with the files for this article was taken from CMS300 version 4.5.0. You should make the above modifications to the version of this file in your CMS installation rather than use the example.
Click here to download the files for this article.