How does creating a canonical URL based on the current URL do any good?
Asked Answered
R

3

8

I have an MVC3 app written in C# that I'd like to generate rel=canonical tags for. In searching SO for ways to achieve this automatically, I came across this post.

I implemented it in my dev environment and it works as intended and generates tags such as

<link href="http://localhost/" rel="canonical" />.

My question is, what good does this do? Shouldn't the canonical URL point to explicitly where I want it to (i.e. my production site), rather than whatever the URL happens to be?

The reason I bring this up is because my hosting provider (who shall remain nameless for now) also generates another URL that points to my site (same IP address just a different hostname, I have no idea why, they claim it's for reverse DNS purposes -- this is another subject). However, I've started seeing my page show up in Google search results under this mirrored URL. Not good for SEO, since it's "duplicate content". Now, I've fixed it by simply configuring my IIS site to respond only to requests to my site's domain, however, it seemed a good time to look at what type of a solution canonical URLs could have provided here.

Using the solution in the post above, the rel=canonical link tag would have output a canonical URL containing the MIRRORED URL if someone were to go to the mirrored site, which is not at all what I would want. It should ALWAYS be <link rel="canonical" href="http://www.productionsite.com" />, regardless of the URL in the address bar, right? I mean, isn't that the point of canonical URLs or am I missing something?

Assuming I'm correct, is there an accepted, generic way to generate canonical URLs for an MVC3 app? I can obviously define them individually for every page, or I can simply replace the rawUrl.Host parameter in the solution I linked with a hard-coded domain name, I'm just wondering why I see so many examples of people generating canonical URLs this way when it doesn't seem to fit the purpose (at least in my example). What problem are they trying to solve by just inserting the current URL into a rel=canonical link element?

Rebba answered 7/4, 2012 at 16:58 Comment(1)
Using the current URL indeed doesn't do much. More typical usage is when slightly different urls (authority or path) give the same page, and the canonical for all of these should be the single preferred / authoritive urlPontonier
L
4

Great question, and you're bang on regarding the mirrored site still getting marked as canonical. In fact, you've got to fix a couple problems before it hammers your "link juice" any harder.

I suspect the main reason is because MVC, by design, is a URL rewriting/routing system. So depending on the massage that occurs to the originally requested URL, people are trying to set the canonical link to the "settled on" final URL format, post rewriting. That said, I think you've dialed in on an oversight most people are having - which is "What about URLs that reached the page, that were NOT anticipated and REWRITTEN to become the valid, canonical path to the URL?" The answer here, is to rewrite these "bad requests" as you discover them. For example: If you rewrote your ISP's mirrored domain requests, then by the time it reaches the loaded page, it's NOW a valid url; This is because it was "fixed" by your rewrite rules. Make sense? So you'll need to update your MVC routes to handle the bad route created by your ISP. NOTE: You MUST make sure you don't use the originally requested URL, but the final, rewritten one, when building the canonical link value.

Continue on for my WWW vs. non-WWW tip, as well as a concern about something you mentioned regarding not processing the invalid urls.

People also do this because your site already "mirrors" another domain that people always forget about. The "WWW" subdomain.

Believe it or not, although debated, many are stating that having www.yourdomain.com/mypage.htm and yourdomain.com/mypage.htm is actually hurting your page ranking due to "duplicated" content. I suspect this is why people are showing the "same domain" there, because it's actually the domain stripped of the "WWW". (I use a rewrite rule to make the www vs no-www consistent.)

Also, be careful regarding "configuring my IIS site to respond only to requests to my site's domain" because if Google still sees links there and considers them a part of your site, it might actually just penalize you for having pages that fail to load (i.e. 404s) I recommend having a rewrite rule that sends them to your "real" domain OR at least have the canonical link be setup to only use your "real" domain with the WWW consistently there, or not there. (It is argued which is better, I don't think it matters as long as you are consistent.)

Loma answered 8/4, 2012 at 1:57 Comment(1)
Scott, one other thought that occurred to me. I should have been specific that rewriting rules are appropriate to a large extent, but this is likely best handled as a combination of a permanent redirect (dealing with invalid domains) AND rewriting (dealing with WWW vs non-WWW, etc.) to reach the ultimate "perfect URL" which you set programatically as your correct, canonical one. Search engines will not penalize you for the redirect, especially if it finally reaches a page that provides the one valid canonical URL in the end.Loma
F
3

What problem are they trying to solve by just inserting the current URL into a rel=canonical link element?

None! They just make things even worse! They have just been mislead

There are misleading answers for this matter, here also on stack overflow which have been accepted and up-voted!!

The whole concept is to produce a unique link ID for each page with different content in a canonical tag.

So a good way to produce unique links for your canonical tags is based on.

Controller Name , Action Name and Language.Those 3 variations will provide different content.

Domain , Protocol and Letter casing don't!

See the question and my answer here for a better understanding.

MVC Generating rel="canonical" automatically

Flute answered 15/12, 2016 at 16:59 Comment(0)
C
0

Creating a canonical URL based on the current URL does NOT do any good. You should create a canonical URL based off something static like database information. For instance, if your URL includes, let's say, the title of a book. You should pull that book title from the database and create the canonical URL from THAT and NOT the current page's URL. That way if part of the URL is missing AND the page still displays, the canonical URL will always be the same.

Conwell answered 22/10, 2015 at 0:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.