# A bout Search engine friendly URL

**URL:** <https://www.ruby-forum.com/t/a-bout-search-engine-friendly-url/120749>\
**Category:** Rails\
**Created:** [November 20, 2007, 10:47am UTC](https://www.ruby-forum.com/t/a-bout-search-engine-friendly-url/120749 "2007-11-20T10:47:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![infinit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/76d3ee/32.png) [@infinit](https://www.ruby-forum.com/u/infinit)\
**Post date:** [November 20, 2007, 10:47am UTC](https://www.ruby-forum.com/t/a-bout-search-engine-friendly-url/120749/1 "2007-11-20T10:47:32Z")

</div>

Hi All,

Can anyone talk about how to generate url like :  
[http://blogs.tech-recipes.com/johnny/2006/08/27/ruby-on-rails-using-full-text-search-with-tagging](http://blogs.tech-recipes.com/johnny/2006/08/27/ruby-on-rails-using-full-text-search-with-tagging)

Is the title string “ruby-on-rails-using-full-text-search-with-  
tagging” set as the primary key or…

if then how to handle the case that in a same day, we hv two articles  
with exactly the same title string.

Thanks in advance.

* * *

Infinit

---

<div class="post-metadata">

**Author:** ![infinit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/76d3ee/32.png) [@infinit](https://www.ruby-forum.com/u/infinit)\
**Post date:** [November 20, 2007, 4:43pm UTC](https://www.ruby-forum.com/t/a-bout-search-engine-friendly-url/120749/2 "2007-11-20T16:43:52Z")

</div>

On Nov 20, 1:46 am, Infinit [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks in advance.
> 
> * * *
> 
> Infinit

there’s a number of plugins to do this:

[http://agilewebdevelopment.com/plugins/search?search=url+slug](http://agilewebdevelopment.com/plugins/search?search=url+slug)

---

<div class="post-metadata">

**Author:** ![infinit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/76d3ee/32.png) [@infinit](https://www.ruby-forum.com/u/infinit)\
**Post date:** [November 20, 2007, 12:31pm UTC](https://www.ruby-forum.com/t/a-bout-search-engine-friendly-url/120749/3 "2007-11-20T12:31:18Z")

</div>

On Nov 20, 2007 9:46 AM, Infinit [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> with exactly the same title string.
> 
> Thanks in advance.
> 
> * * *
> 
> Infinit
> 
> >

This is done by using a custom named route, as designated in your  
config/routes.rb file.

A good way of explaining how this particular example  
([http://blogs.tech-recipes.com/johnny/2006/08/27/ruby-on-rails-using-full-text-search-with-tagging](http://blogs.tech-recipes.com/johnny/2006/08/27/ruby-on-rails-using-full-text-search-with-tagging))  
may have been formed using Rails, would be this (a possible line in  
the routes.rb file):

map.post ‘:user/:year/:month/:day/:title’, :controller =\> ‘posts’,  
:action =\> ‘show’

This will route any request that matches that format (i.e.  
/johnny/2006/08/27/ruby-on-rails-blah-blah) to the ‘show’ action under  
your ‘posts’ controller. The values you specify as each ‘segment’ of  
the uri (in this example, you are using ‘johnny’ under the :user and  
2006 as the :year), will be passed to your controller under the  
‘params’ hash, allowing you to access them from within the controller.  
An example in your controller would be:

@post = Post.find(:first, :conditions =\> [:author =\> params[:user],  
:subject =\> params[:title]])

[If anything I’m typing is wrong, please let me know everyone]. As you  
can guess, this would allow you to pass the values in the (nicely  
formed) URL to your controller, and work with it as you like.

In response to your second question; no, the title would not be the  
primary key here. The primary key is actually not included in the URL  
at all (unless you form a composite primary key, which would only seem  
sensible to use all the segments together, but that’s probably not  
very efficient), and you would have to write a find statement to  
search your database for the first post that matches your input –  
because of this, you run the risk of having two posts named exactly  
the same posted on the same day, but only one would be found. If this  
is the case, then a URL format like this obviously would not suffice.  
You would have to add your own identifier on the end, perhaps:

map.post ‘:user/:year/:month/:day/:title/:iterator’, :controller =\>  
‘posts’, :action =\> ‘show’, :iterator =\> 1

(Note that the default value for your :iterator parameter can be set  
in the routes file too, so if it is not set in the URL, it will  
default to 1. You can also add validation on your params here, but  
that is for a later e-mail =P).

This was, you can tell your ‘find’ statement to skip a certain number  
of posts (from the same day, with the same name) in order to find the  
one you want.

I hope this helps and I’m not completely missing the point of your  
question. Also, if any rails ninjas superior to me spot any  
inefficiencies or mistakes please call me out on it.

–  
Edd Morgan  
[http://www.eddm.co.uk](http://www.eddm.co.uk)  
+44 (0) 7805 089097

---

<div class="post-metadata">

**Author:** ![infinit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/76d3ee/32.png) [@infinit](https://www.ruby-forum.com/u/infinit)\
**Post date:** [December 1, 2007, 11:11am UTC](https://www.ruby-forum.com/t/a-bout-search-engine-friendly-url/120749/4 "2007-12-01T11:11:34Z")

</div>

Thanks for all your replies.

---

<div class="post-metadata">

**Author:** ![infinit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/76d3ee/32.png) [@infinit](https://www.ruby-forum.com/u/infinit)\
**Post date:** [December 1, 2007, 1:10pm UTC](https://www.ruby-forum.com/t/a-bout-search-engine-friendly-url/120749/5 "2007-12-01T13:10:27Z")

</div>

From looking at them all a few months back,  
[http://www.railslodge.com/plugins/4-acts-as-friendly-param](http://www.railslodge.com/plugins/4-acts-as-friendly-param) seems to tie  
one of the best plugins to achieve something similar at the minute

Infinit wrote:

> Hi All,
> 
> Can anyone talk about how to generate url like :  
> [http://blogs.tech-recipes.com/johnny/2006/08/27/ruby-on-rails-using-full-text-search-with-tagging](http://blogs.tech-recipes.com/johnny/2006/08/27/ruby-on-rails-using-full-text-search-with-tagging)
> 
> Is the title string “ruby-on-rails-using-full-text-search-with-  
> tagging” set as the primary key or…

---

<div class="post-metadata">

**Author:** ![infinit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/76d3ee/32.png) [@infinit](https://www.ruby-forum.com/u/infinit)\
**Post date:** [November 20, 2007, 5:32pm UTC](https://www.ruby-forum.com/t/a-bout-search-engine-friendly-url/120749/6 "2007-11-20T17:32:09Z")

</div>

The basic idea is just to override the to\_param method for the id of  
the model object being referenced. If to\_param returns a number  
followed by a hyphen and a string, then that is what will be displayed  
when a URL is generated that references that ID. And when the ID is  
passed to a find method, it is automatically converted to an integer,  
which strips the string and leaves just the numeric ID. The various  
plugins that are available just simplify this by providing processing  
for the string field to make it safe to use in a URL (for example, by  
replacing spaces with hyphens).

Michael  
[www.buildingwebapps.com](http://www.buildingwebapps.com)
