推荐的博客平台的Url格式

时间:2012-05-28 10:33:47

标签: ruby-on-rails ruby api url rest

我正在构建一个博客平台(RoR),并计划使用以下格式,有任何缺点吗?

# All Users:
http://www.example.com/users/

# A single user (123 is the user id, id is needed for uniqueness)
http://www.example.com/users/123/peter

# All Categories
http://www.example.com/categories/

# A single category listing (123 is the cat id, note: job is singular)
http://www.example.com/categories/123/job

# All Tags
http://www.example.com/tags/

# A single tag listing (123 is the tag id, note: car is singular)
http://www.example.com/tags/123/car

# A single post
http://www.example.com/posts/123/my-title

有任何建议或改进的地方吗?

感谢。

5 个答案:

答案 0 :(得分:5)

这完全取决于您应用中最重要的主题。假设应用程序的主要主题是帖子,那么帖子的URL必须“更接近”根URL。

所以他们看起来像这样:

# A single post
http://www.example.com/123-my-post-title

# All Users:
http://www.example.com/users/

# A single user (supposing the username must be unique)
http://www.example.com/users/peter

# All Categories
http://www.example.com/categories/

# A single category listing (supposing the category name must be unique)
http://www.example.com/categories/job

# All Tags
http://www.example.com/tags/

# A single tag listing (supposing tags must be unique)
http://www.example.com/tags/car

这涉及到2条路线的变化:

1)删除网址中的“/ posts /”。为此,您可以声明您的资源:

resources :posts, :path => "/"

2)除了post#show之外的所有URL中的id(不同用户的帖子有时可能会有相同的标题,如果没有,你也可以在这些情况下省略ID)。

要做到这一点,应该覆盖模型中的to_param方法,如下所示:

    class Post < ActiveRecord::Base
      def to_param
        "{id}-#{title.parameterize}"
      end
    end

或如果您发现覆盖to_param困难,请使用friendly_id gem

答案 1 :(得分:1)

我遇到了同样的问题,而且我遇到了一个伟大的宝石,使你的网址SEO和人类友好:friendly_id

这是一个很棒的截屏视频:Pretty URLs with FriendlyId

我不打算在这里详细介绍,因为我相信你会在文档或截屏视频中找到所需的一切。快乐的SEO!

答案 2 :(得分:0)

由于(我假设)用户拥有唯一的昵称,因此网址可以更简单:

http://www.example.com/users/peter

同样适用于标签和类别。您不要忘记根据参数化检查名称的唯一性。

它还有助于避免重复具有相似名称的对象:

"peter the great".parameterize => "peter-the-great"
"Peter-THE-Great".parameterize => "peter-the-great" 

这只是我的观点,在大多数情况下使用了具有唯一id-nameid/name的逻辑(请查看此问题的网址:)。

答案 3 :(得分:0)

我承认我对SEO的了解并不像StackOverFlow上一些更有经验的人那么深远。但是我确实知道,当归结为URL时,'更短且可读性越好'规则适用。因此,在URL中使用id是一个很大的挫折,可能会对谷歌或其他搜索引擎的搜索引擎优化得分产生负面影响。

你可以在谷歌的免费SEO入门指南中阅读相关内容,你可以在这里找到PDF:http://static.googleusercontent.com/external_content/untrusted_dlcp/www.google.com/nl//webmasters/docs/search-engine-optimization-starter-guide.pdf

有关网址的部分位于此PDF的第8页。

至于您的应用程序,避免使用ID的方法是为您的用户使用昵称。您会发现这在网络上的论坛上常用。这些昵称始终具有唯一的限制,确保始终只有一个用户具有该昵称。它还使您可以为这些昵称创建限制,例如禁止在昵称中使用数字,这样人们就无法创建像John123这样的昵称。

最后你想得到一个像这样的网址:

http://www.example.com/users/uniquenickname

同样的想法是使用类别和标签。

当它归结为帖子时,你有两个选项,实际上取决于你的帖子的确切含义。 如果通过帖子您只是意味着博客条目,那么您可以简单地使用条目的类别,用户和标题的组合来创建唯一的URL。

例如:http://www.example.com/category/uniquenickname/entrytitle

如果您在博客条目中发表评论时发布帖子,那么使用您在上面提供的网址就可以了,但您可能希望在robots.txt中添加不允许抓取工具索引这些网址的内容。 这背后的原因是你往往有垃圾邮件机器人围绕博客发布广告和链接到你绝对不希望与之关联的网页,并且你想阻止抓取工具索引这些链接,这会对你的网站产生负面影响搜索引擎优化得分。

我希望这会有所帮助:)

答案 4 :(得分:-1)

我非常自私和自豪,所以尽量使我的网站难以抓取:P,尽量不要在你的网址上使用数字ID。它使你很容易刮掉整个网站。 尽可能制作独特的网址。

如果您的帖子标题是唯一的,请将其设为ID并在您的网址中使用。

如果您的用户名是唯一的,请将其用作ID。例如:

#所有用户:     http://www.example.com/users/

# A single user 
http://www.example.com/users/peter

# All Categories
http://www.example.com/categories/

# A single category 
http://www.example.com/categories/job

# All Tags 
http://www.example.com/tags/

# A single tag listing (They are unique, each one should be a key itself)
http://www.example.com/tags/car

# A single post
http://www.example.com/posts/date-here/my-unique-title

Google喜欢至少包含位数的网址。将日期添加到新闻网址是一个很好的做法,它也增加了SEO值。像这样http://www.example.com/posts/2012/06/06/my-unique-title