Canonicalizing DotNetNuke URLs in ASP.NET 3.5

Do you have canonical issues with the your DotNetNuke website's URLs? These issues can lead to your site not getting proper credit in Google for all of the links it has earned, thus reducing its standing on the search engine results pages. This means reduced visibility and decreased traffic. This problem, though serious, is relatively easy to fix; keep reading to find out how.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 4
August 09, 2010
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Duplicate content or canonical URL issues are a big problem in onsite DotNetNuke SEO. This tutorial will correct these problems in your default DotNetNuke website and installed blog module. Specifically, the VB.NET script at the end of the tutorial will correct the following problems:

Issue number one: Canonical issues among /, /home.aspx, /default.aspx and their upper case versions /Home.aspx, /Default.aspx, and even entire upper case versions e.g /HOME.ASPX

Google treats lower and upper case URLs differently (it is case sensitive); because of this, it is very possible that a DotNetNuke blog may have some canonical URL issues. There have been many recorded instances of case sensitivity causing problems in the URL -- specifically, that it can cause issues in Google: http://bit.ly/d9TKuh

This problem can be solved by assigning these following URLs with its canonical version: /

For example:

http://www.thisisyourdotnetnukewebsite.com/Defaullt.aspx

http://www.thisisyourdotnetnukewebsite.com/default.aspx

http://www.thisisyourdotnetnukewebsite.com/Home.aspx

http://www.thisisyourdotnetnukewebsite.com/home.aspx

The canonical URL is simply: http://www.thisisyourdotnetnukewebsite.com/

Issue number two: Problems with lower and upper case URLs similar to those illustrated above. For example, the URLs below will return a 200 OK header status:

http://www.thisisyourdotnetnukewebsite.com/Privacy.aspx

http://www.thisisyourdotnetnukewebsite.com/privacy.aspx

http://www.thisisyourdotnetnukewebsite.com/PRIVACY.ASPX

If there are back links pointing to them, Google will treat them as different URLs which can cause canonical issues. To solve this problem, use the lower case version consistently throughout your URLs. So the canonical version will be:

http://www.thisisyourdotnetnukewebsite.com/privacy.aspx

There are many options for solving canonical issues, such as doing a 301 redirect, but that can be too complex. The simple solution to canonical issues is using what is known as a “link rel canonical tag.” 

To use link rel canonical tag, simply assign the canonical URL to the href value. For example, using the scenarios illustrated above:

<link rel='canonical' href='http://www.thisisyourdotnetnukewebsite.com/privacy.aspx' />

<link rel='canonical' href='http://www.thisisyourdotnetnukewebsite.com/' />

 

Issue number three: If you have installed a blog module in DotNetNuke, the problem will worsen because the same issues above will also appear for the blog.

This tutorial will work only if you are using a URL-friendly structure for your blog, such as:

http://www.thisisyourdotnetnukeblog.com/Blog.aspx (for Blog front page)

http://www.thisisyourdotnetnukeblog.com/Blog/tabid/61/EntryId/3/This-is-really-interesting-my-THIRD-POST-is-now-published.aspx (for Blog post)

http://www.thisisyourdotnetnukeblog.com/Blog/tabid/61/catid/1/For-NewBie-Only.aspx (for sample blog category)

So for example, if your domain name is abcefg.net and your DotNetNuke URL structure is similar to the one used above, for example:

http://www.abcefg.net/Home.aspx (for home page)

http://www.abcefg.net/Terms.aspx (a sample page)

http://www.abcefg.net/Blog/tabid/61/EntryId/1/Thsi-is-my-first-blog-post.aspx (sample blog post)

http://www.abcdefg.net/Blog/tabid/61/catid/2/My-imaginations.aspx (sample category URLs)

Then you can implement the script as suggested in this tutorial to sort out canonical issues in your DotNetNuke website and blog.

This tutorial uses VB.NET as the programming language for DotNetNuke on the ASP.NET 3.5 platform. The DotNetNuke version tested is 05.02.00 (275).

Fixing canonical issues with home page URLs

The complete script and installation instructions are found at the end of this tutorial. It is recommended that you follow this tutorial carefully so that you can  further customize it on your own.

Let’s fix the home page canonical issues first (the first issue described in the previous section).

If you have launched your DotNetNuke website files in a local host environment using a web editor such as Visual Web Developer 2008 Express Edition, you will see a file named “Default.aspx.vb.” This is the Visual basic script that you will need to edit and into which you will incorporate the canonical script.

You need to execute the canonical script as a page load event. This means that, if the URL has been requested by the server, and then loaded into the browser, the script will be executed automatically and output the required link rel canonical tags to the browser.

In Default.aspx.vb, the page load event handler can be found under:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 

Let’s insert the canonical script as the first script to be executed. First, let’s declare some variables (you will know their use later):

Dim urlofpage As String

Dim canonicalurl As String

Dim blogposturlcheck As Integer

Dim requesturl As String

Dim linkrelcanonicalcode As String

Dim blogcategoryurlcheck As String

 

The next step is to get the requested URL:

requesturl = Request.Url.ToString()

 

Now we need the Global DotnetNuke URL. This is the rewritten URL, not just the requested URL. It's the one you will see in the browser address bar. 

urlofpage = DotNetNuke.Common.Globals.NavigateURL()  

Below is the distinction between the request url and urlofpage.

http://www.thisisyourdotnetnukewebsite.com/Default.aspx?TabID=40 is a request URL; this URL has not been rewritten.

This is the equivalent urlofpage (rewritten, clean): 

http://www.thisisyourdotnetnukewebsite.com/Home.aspx

Note that the values are assigned to the variables declared earlier. In a VB.NET script, you cannot use variables without declaring them first, because it is a strongly-typed language.

Now that you have the rewritten DotNetNuke URL assigned to the urlofpage variable, you need to convert it entirely to lower case for consistency.

urlofpage = LCase$(urlofpage)  

Finally, you can execute an IF statement to test if the URL is one of the home page URL variants:

If urlofpage = "http://localhost/dotnetnukewebsite/home.aspx" Then

canonicalurl = "http://localhost/dotnetnukewebsite/"

linkrelcanonicalcode = "<link rel='canonical' href='" & canonicalurl & "' />"

'Output canonical URL

linkrelcanonical.Text = linkrelcanonicalcode

End If

 

You need to replace the localhost URLs with your own URL and domain during testing and implementation. Also, make sure it outputs to the browser using the linkrelcanonical id in Text.

In your Default.aspx code, add the line below just after <head id="Head" runat="server"> :  

 

<asp:Literal id="linkrelcanonical" runat="server" />

 

 

This is necessary because the the link rel canonical element should be outputted to the <head> section of the web HTML code.  

Fixing canonical issues for the rest of your pages

Now that the home page's canonical URL issues has been sorted out, let's move on to sort out the rest of the URLs, except for the blog. The blog needs special treatment, which will be discussed in the next section.

First, you need to make sure that the requested URL is not a blog post URL or a blog category URL:

blogposturlcheck = InStr(1, requesturl, "EntryId", vbTextCompare)

blogcategoryurlcheck = InStr(1, requesturl, "catid", vbTextCompare)

 

The InStr function will simply compare to see if the the requested URL contains one of those strings. (catid is unique for categories, while EntryId is unique for post).

If there is no match (meaning the URL is NOT a blog post or a category URL), it will return a value of zero.

Finally, the IF statement will be executed to solve the lower and upper case issues between other URLs, except for home.aspx, default.aspx, / , Blog Post URL:

If urlofpage <> "http://localhost/dotnetnukewebsite/home.aspx" And blogposturlcheck = 0 And blogcategoryurlcheck = 0 Then

canonicalurl = urlofpage

linkrelcanonicalcode = "<link rel='canonical' href='" & canonicalurl & "' />"

'Output canonical URL

linkrelcanonical.Text = linkrelcanonicalcode

End If

 

The IF statement will check first to see if the DotNetnuke URL is not a home page and not a blog post/category URL. If it is not, then the lower case version of the URL will be assigned to the canonicalurl variable, which will then be outputted to the browser link rel canonical element.

Fixing canonical issues for the DotNetNuke Blog module

The script will only tackle blog post URLs, because category URLs cannot be called “canonical URLs” and can be tagged with <meta name="robots" content="noindex"> programmatically as a simpler solution. This will let the search engine bots crawl the page, but not index it, preventing any canonical/duplicate content issues.

This is the trickier part of the script; you will use the requesturl variable for this one, and extract the ID of the post from the URL.

Example: if this is the post URL (requesturl):

http://localhost/dotnetnukewebsite/Default.aspx?TabId=61&EntryId=3

The script will extract the post ID, which is 3. Once the ID has been extracted using URL string manipulation, the permalink URL (canonical search engine friendly URL version of the post) can simply be retrieved from the MS SQL server database. The permalink URL will then be outputted to the browser link rel canonical tags.

Code Discussion in italicized comments below:

'First, count the entire length of the URL required for URL string manipulation to  extract the post and category ID.

 

Dim value As Integer

value = requesturl.Length

 

'Second, make sure you are dealing with blog post URLs in DotNetNuke.

 

If blogposturlcheck <> 0 Then

 

'Third, here is the start URL string manipulation for post id.

 

Dim startpositionblogurl As Integer

startpositionblogurl = 7 + blogposturlcheck

Dim endpositionblogurl As Integer

endpositionblogurl = value - startpositionblogurl

Dim blogid As String

blogid = requesturl.Substring(startpositionblogurl, endpositionblogurl)

 

'Fourth, retrieve the DotNetNuke permalink URL from the database.

 

Dim myCommand As SqlCommand = New SqlCommand()

myCommand.Connection = New SqlConnection("Data Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|Database.mdf;Integrated Security=True;User Instance=True")

myCommand.Connection.Open()

myCommand.Parameters.AddWithValue("@blogid", blogid)

myCommand.CommandText = "SELECT PermaLink FROM Blog_Entries WHERE EntryID=@blogid"

Dim retrievevalue As SqlDataReader = myCommand.ExecuteReader()

While retrievevalue.Read()

Dim permalinkpost As String

permalinkpost = CStr(retrievevalue("PermaLink"))

 

'Fifth, convert the post URL entirely to lower case.

 

permalinkpost = LCase$(permalinkpost)

canonicalurl = permalinkpost

 

'Sixth, output the canonical URL to the browser link rel canonical tags and close the MS SQL connection.

 

linkrelcanonicalcode = "<link rel='canonical' href='" & canonicalurl & "' />"

linkrelcanonical.Text = linkrelcanonicalcode

End While

retrievevalue.Close()

myCommand.Connection.Close()

End If

Implementation

You can download the complete script here: http://www.dotnetdevelopment.net/tutorials/dotnetnukecanonicalscript.txt

To implement, go to the Default.aspx.vb file of your DotNetNuke website and find this line:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

 

Paste the canonical script next to this, as shown in the screen shot below:

 

Note: you should never touch, overwrite or delete any lines in the Defaut.aspx.vb, except for just adding this canonical script.

Replace the following items below:

http://localhost/dotnetnukewebsite/home.aspx WITH your own domain name, for example: http://www.thisisyourdotnetnukewebsite.com/home.aspx 

http://localhost/dotnetnukewebsite/ WITH your own domain name also, for example: http://www.thisisyourdotnetnukewebsite.com/

Since you are deploying your website in a hosting environment for publishing on the Internet, you should also be changing the SQL connection parameters below:

myCommand.Connection = New SqlConnection("Data Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|Database.mdf;Integrated Security=True;User Instance=True")

For details about connection strings, you can either contact your web host, or ask for DotNetNuke support for deploying the website with your web hosting company. Or you can just refer to this guide

If you are using MS SQL Server 2008 for your DotNetNuke website, you can get more help and information here: http://www.connectionstrings.com/sql-server-2008. It depends on what is supported by your hosting company, and such details are out of the scope of this tutorial.

Once you have saved your changes, test your site in your browser. It is highly recommended that you start with a local host environment to fully test this script before deploying it with your web host.

Note: It is highly recommended that you back up all affected files first, before making any changes.

blog comments powered by Disqus
ASP.NET ARTICLES

- Implementing ASP.NET 4.0 Page.MetaDescriptio...
- ASP.Net Development Tips
- Intro to Sessions in ASP.Net
- Google Maps API Introduction in ASP.NET usin...
- Creating an ASP.NET 3.5 Gridview Image Galle...
- Encrypt QueryString in ASP.NET 3.5 using VB....
- ASP.NET 3.5 Drop Down List Controls
- Connect to Access Database with ASP.Net
- Secure Audio Streaming with ASP.Net and Flash
- Dynamic Sitemap and Navigation in ASP.Net
- Implement Gzip and Deflate Compression in AS...
- Run ASP.Net in Ubuntu with Apache
- ASP.Net Mono Website Contact Forms
- ASP.Net URL Rewriting Methods
- Murach`s ASP.NET 4 Web Programming with C# 2...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials