开发者

SEO Friendly URLs where the phrase used may change in Rails

Say I have http://www.mysite.com/I-Like-Cheeseburger开发者_如何学JAVAs and I want that to point to Item with id 3. Sometime later, I change the name of the item, and now its http://www.mysite.com/I-Like-Hamburgers (and perhaps many more times). I want all these URLs to remain pointing to Item 3. Is it efficient to simply keep a table of [strings,item_ids] and do a lookup on this? Is there a better way?


Or you can do like stackoverflow.com

http://stackoverflow.com/questions/2887068/seo-friendly-urls-where-the-phrase-used-may-change-in-rails

You keep the question ID (2887068) in the link, where the text part can be changed and still will land in the same page. http://stackoverflow.com/questions/2887068/whatever-you-put or http://stackoverflow.com/questions/2887068/ will send you to the same page.

so for your example it would be :

http://www.mysite.com/items/3/I-Like-Cheeseburgers


You can also use plugin friendly_id.

With :use_slug option it maintains history of permalinks and offer solutions to redirect to current URL etc.

class YourModel < ActiveRecord::Base
  has_friendly_id :title, :use_slug => true
end

See documentation for more.


in your model do

class Article < AR::Base
  def to_param
    "#{id}-#{name.parameterize}"
  end
end

Your links will look like: http://mysite.com/articles/3-i-like-cheeseburgers

When you try to fetch the object, doing Article.find(params[:id]) would automagically convert the "3-i-like-cheeseburgers" into an integer and fetch the correct object. This allows the name to change, while maintaining the link to point to the same article object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜