|
Intro This article explains URL masking / server side redirection that is in use on many of the latest websites. I will walk through the steps required to implement URL masking on a .NET website. The technology code snippets relate to Microsoft C#.NET 3.5 Purpose The purpose of URL redirection is to allow logical pages, which do not physically exist on the hard disk of the web server, to be accessible under a website.
Consider the following example URL as an example: http://www.my-test-website.co.uk
The domain name must actually exist, but the following path doesn't: http://www.my-test-website.co.uk/Hosting/My-Test-Web-Page.aspx
When you navigate to this webpage, the server will attempt to locate the page My-Test-Web-Page.aspx in the folder Hosting on the hard disk of the web server. When it looks for it, it will not find the page on the hard disk and will return an error 404: Not found to the user’s browser. An example of the default .NET 404: Not found error looks like the following:
What we want to achieve from this is the user typing in a page / URL that doesn’t exist and being presented with a page.
The benefit of this is mainly for SEO purposes. If you were to simply redirect the user to a different page e.g. Response.Redirect () the search engine bot will detect the redirection and not provide any link benefit to the page originally requested.
URL masking allows an internal / server redirect that happens without any search engine bot knowing about it. The key benefit of this is that the creation of new pages that are SEO compliantly named pages is very efficient. In the examples provided here, it simply requires a new record into the database.
The page(s) you can use to server side redirect the user to could be one that isn’t SEO optimised by uses querystrings to change the view of the page, this is another benefit of URL masking. For example,
URL Requested: my-first-seo-page.aspx URL Redirected to: article.aspx?articleid=123
URL Requested: my-second-seo-page.aspx URL Redirected to: article.aspx?articleid=201
All we have done here is use the article.aspx page for both redirects and simply use a different querystring based on mapping from our datasource.
Implementation requirements The implementation requirements will be based on the requirements of the example implementation provided in this article. - Data source; database
- .NET 3.5
Datasource Before we create the code to implement the URL rewriting, we must setup a datasource, in our case a database table.
The table only needs to include 2 fields: Requested Page and Actual Page
With these fields, we map pages that are requested by a user to the actual page on the server. We will call upon this table in the following section to check if what page the user request matches a mapping that we have setup.
HTTP Module A popular approach is to use a HTTP module to implement the URL masking technology. Combining this approach with a .NET class library (which compiles as a .dll file to include in any project) allows efficient reusability of the technology.
Create a new solution and an empty C# class library project in Visual Studio 2008. Add a single c# class to the project.
The c# class you just created needs to inherit from the System.Web.IHttpModule class – ensure that your project and your class includes a reference to it;
There are 2 methods that the class must implement:
public void Init(System.Web.HttpApplication application) public void Dispose()
The Dispose method you can leave unimplemented but the Init method is what the website calls first, so it needs to setup a call to a new special method:
private void Application_OnAfterProcess(Objectsource, EventArgs e)
This method is where the magic happens. We do not need to worry about the EventArgs parameter of this method but it still needs to be defined because we will define this method as an event using the System.EventHandler constructor (which expects a method with both Object and EventArgs parameters).
The first thing we need to do with this method is create a new variable to store the HttpApplication data which is being passed in by the source object, so that we can access it later on.
With the HttpApplication variable, first we want to check that the file (the web page requested) doesn’t already exist. The best way to do this is calling the Request object under the HttpApplication object and check the AbsolutePath property. This will give you path the file relates to on the server.
You can use the AbsolutePath string with a simple check using System.IO.File.Exists method to check the file exists.
If the file exists, then the following code will not be executed and the site will continue with normal execution.
Upon the condition that it doesn’t exist, we want to check our datasource to determine if we have a virtual page setup.
The easiest way to this is to run a simple SQL query against the database table that we setup in the previous step, checking the requested page against the RequestPage field in the database table. If there are any rows returned we have a match.
The key ingredient with all of this technology is the Application.Context.RewritePath(string) method, which allows the server side redirect to take place. After this point, no other code needs to execute in the class.
When the page contents are returned to the user, the page will load but the address bar in users browser will remain the same as what they requested. This is a small aesthetic benefit that will improve the look and feel of the website.
Compilation and setup in the website After the code is written and compiled, you will need to build the class library. Locate the build location for the class library and find the DLL that has been created. Copy this file into the website project you wish to add the URL masking to.
Adding the DLL as a reference to the website can be done by right clicking on the website project, clicking Add Reference, and go to the browse tab to locate the DLL on your computer.
The last setup step is to tell the website that we have a HTTP Module that we wish the site to use. Open up your web.config and locate the block, it should already exist with the ScriptModule Http Module added.
Add a new entry using the following syntax:
<add name="UserDefinedName" type="Namespace.Class, Class" />
For the UserDefinedName, it is best to use the name of the class to avoid any confusion about what the module is used for. Ensure you setup a request and actual page record in the database table you setup, recompile the website and it should be working.
|