mongoid / mongoid-slug

Generates a URL slug/permalink based on fields in a Mongoid-based model.
https://github.com/mongoid/mongoid-slug
MIT License
492 stars 164 forks source link

Check if slug is the active one or a old one #106

Closed mattiassvedhem closed 11 years ago

mattiassvedhem commented 11 years ago

i.e check if the slug is the last one in the array I guess. Would you pull it in if I made a pull request or is it out of the scope of the library?

Seems like there are a lot of reasons why one would like to check it. Mine is to add a noindex to the old slugs to prevent duplicate content in search engines.

mattiassvedhem commented 11 years ago

Oh, ofc, there's still a slug method, so maybe it's too trivial..

def active_slug?(slug)
  self.slug == slug
end
digitalplaywright commented 11 years ago

If we are to merge in functionality like this we should have a wiki-entry with best practices on avoiding duplicate indexing by search engines. However, are you sure meta tags does not solve this problem of uniquely identifying a piece of content?

E.g:

%link{:rel => "alternate", :href => original_url_here, :title => some_title, :type => "application/xml+oembed"}/
%meta{:property => "og:url", :content => original_url_here }/
%meta{:name => "twitter:url", :content => original_url_here  }/

Here 'original_url_here' is an url that is built with BSON::ObjectId and not the slug. In this way it doesn't matter what the slug is now or if it is changed in the future, and documents can be shared using a pretty url and indexed using an alternative url.

Let me know what you think. I do not know if the major search engines are smart enough to deduplicate content using the alternative url, but facebook and twitter is smart enough to do so.

mattiassvedhem commented 11 years ago

I see what you mean, but wouldn't such a URL be "less worth" to search engines, since it doesn't include the slug, which is one of the problems the slugs solves in the first place? Or maybe they don't care about that for such meta tags.

However, it would mean that the links on google would include the object id and not the slug, right?

Btw, I think that canonical URL should be used for what you are describing. However I think Matt Cutts described the tool order in which to prevent duplicate content in one of his videos. And as far as I can remember Canonical URL was the last resort.

mattiassvedhem commented 11 years ago

On further thought, I think canonical would be the right way to solve it, since just putting up no index meta wouldn't attribute the correct link.

I think something like this would be a good way to do it.

<% unless item.active_slug?(params.fetch(:id)) %>
  <link rel="canonical" href="<%= item_url(item.slug) %>"/>
<% end %>

Which might be turned into

<%= canonical_link_for(item, params.fetch(:id)) %>
digitalplaywright commented 11 years ago

I am happy that this solution works. :) I'll add the active_slug helper to mongoid slug.

digitalplaywright commented 11 years ago

On second though. Why is it not sufficient to just call to_param on the model? Like e.g (untested code):

<% unless item.to_param() == params.fetch(:id) %>
  <link rel="canonical" href="<%= item_url(item.slug) %>"/>
<% end %>
mattiassvedhem commented 11 years ago

I guess that would do it too, yeah. When using canonical I'm not even sure that we need to omit the link for the correct slug. So the check might be unnecessary.

digitalplaywright commented 11 years ago

If might be that the conditional is unnecessary if search engines and crawlers are clever enough to detect cyclic canonical references. I do not know if that is true, but if you figure it out please report back here so that others that search up this issue can figure it out.

Also, if you have time please add a wiki-entry on the topic. If you do not have time I can add it myself later this week.