HomeASP.NET Canonicalizing DotNetNuke URLs in ASP.NET ...
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 Codex-M Rating: / 4 August 09, 2010
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: /
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:
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:
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:
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)
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.
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
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"> :
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:
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
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.
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.
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 AsInteger
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.
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) HandlesMyBase.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.