# SEO-friendly urls: Replacing the '-' with '%20'

**URL:** <https://www.ruby-forum.com/t/seo-friendly-urls-replacing-the-with-20/113472>\
**Category:** Rails\
**Created:** [August 31, 2007, 12:57pm UTC](https://www.ruby-forum.com/t/seo-friendly-urls-replacing-the-with-20/113472 "2007-08-31T12:57:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dohmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/439d5e/32.png) [@dohmer](https://www.ruby-forum.com/u/dohmer)\
**Post date:** [August 31, 2007, 12:57pm UTC](https://www.ruby-forum.com/t/seo-friendly-urls-replacing-the-with-20/113472/1 "2007-08-31T12:57:42Z")

</div>

Hi, I’m currently getting to grips with routing in rails, and it’s going  
pretty well. However, I have a small issue with inputting elements into  
the url which are more than one word, for example

[www.example.com/recipe/rice-and-cheese](http://www.example.com/recipe/rice-and-cheese)

is how I’d like to format the url that is typed into the url, however,  
the problem I am having is how to to convert the query part  
(rice-and-cheese) into this (“rice and cheese”)… I’ve tried using gsub  
within routes, with no success.

How do you do this in routing? Or does it have to be don by the action  
that is handling the query parameter?

---

<div class="post-metadata">

**Author:** ![dohmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/439d5e/32.png) [@dohmer](https://www.ruby-forum.com/u/dohmer)\
**Post date:** [August 31, 2007, 6:55pm UTC](https://www.ruby-forum.com/t/seo-friendly-urls-replacing-the-with-20/113472/2 "2007-08-31T18:55:18Z")

</div>

Assuming I’m reading you correctly – which appears to be that you  
want a recipe called ‘Rice and Cheese’ to become a url of ‘/recipes/  
rice-and-cheese’ – then you want something like the PermalinkFu  
plugin ( [http://svn.techno-weenie.net/projects/plugins/permalink\_fu](http://svn.techno-weenie.net/projects/plugins/permalink_fu) )  
by Rick Olsen.

With that you can add (to the Recipe model):

has\_permalink :name

and

def to\_param; permalink; end

Then add a permalink column with a migration and replace the  
Recipe.find(params[:id]) with Recipe.find\_by\_permalink(params[:id]) in  
your RecipeController.

PermalinkFu takes care of converting all the spaces to dashes, as well  
as removing other non-word characters and also does a UTF-8 to ascii  
translation, so your creme brulée recipe would have a permalink of /  
recipes/creme-brulee.

Alternately, assuming that you meant query literally – or if  
permalink\_fu is too much, then yes. The way to handle it would be in  
the Controller or Model rather than in the routes. Probably the  
easiest way is to call split(“-”) on the params[:id] (or whatever your  
route supplies) and then do an appropriate find from there.

Regards,  
Jon

---

<div class="post-metadata">

**Author:** ![dohmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/439d5e/32.png) [@dohmer](https://www.ruby-forum.com/u/dohmer)\
**Post date:** [August 31, 2007, 1:28pm UTC](https://www.ruby-forum.com/t/seo-friendly-urls-replacing-the-with-20/113472/3 "2007-08-31T13:28:26Z")

</div>

I have done this for SEO reasons on my application [soccertackle.com](http://soccertackle.com)  
for products and categories

in the routes.rb file i have created

# named routes for categories

map.connect ‘:controller/:action/:id/:category’, :controller =\>  
“shop”, :action =\> “list”

# named routes for products

map.connect ‘:controller/:action/:id/:item’, :controller =\>  
“shop”, :action =\> “view”

and have used an application\_helper.rb method using gsub to change the  
format of the :item or :category parameter in the url and called this  
method in my views.

I’m sure that this is not the best way to do it but it does mean  
search bots see a different page for every product and category in the  
shop and the title of the product or the category is the name of that  
page instead of realising that it is one dynamically generated page .

Of corse you can still access the pages without the :category or :item  
parameter on the end of the url

I hope this helps and will be checking back to see if any one has any  
better ideas.

On Aug 31, 11:57 am, Paul J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
