# How to make will\_paginate generated URL search engine friendly

**URL:** <https://www.ruby-forum.com/t/how-to-make-will-paginate-generated-url-search-engine-friendly/121820>\
**Category:** Rails\
**Created:** [December 1, 2007, 11:19am UTC](https://www.ruby-forum.com/t/how-to-make-will-paginate-generated-url-search-engine-friendly/121820 "2007-12-01T11:19:46Z")\
**Posts on this page:** 4\
**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:** [December 1, 2007, 11:19am UTC](https://www.ruby-forum.com/t/how-to-make-will-paginate-generated-url-search-engine-friendly/121820/1 "2007-12-01T11:19:46Z")

</div>

I m using will\_paginate plugin to do the pagination. And I find that  
the generated URL is like the following format:

[http://localhost:3000/site?page=2](http://localhost:3000/site?page=2)  
[http://localhost:3000/site?page=3](http://localhost:3000/site?page=3)  
…

how can I let it generate URLs like the following format:(or any other  
formats which is search engine friendly).

[http://localhost:3000/site/page/2](http://localhost:3000/site/page/2)  
[http://localhost:3000/site/page/4](http://localhost:3000/site/page/4)

appreciate for your help.

---

<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, 8:28pm UTC](https://www.ruby-forum.com/t/how-to-make-will-paginate-generated-url-search-engine-friendly/121820/2 "2007-12-01T20:28:10Z")

</div>

this is how I have it working. i’s not necessarily the best way. I’m  
a newbie.

in routes.rb, after map.resources :posts

# anything like /2 (just a page#) should go to my default controller

posts

map.connect ‘:page’,  
:action =\> ‘index’,  
:controller =\> ‘posts’,  
:requirements =\> { :page =\> /\d+/},  
:page =\> nil

# anything like /posts/tags/rails/2 should go to the appropriate

controller and action

map.connect ‘:controller/:action/:id/:page’,  
:requirements =\> { :page =\> /\d+/}

Note: there’s no page =\> nil here. page must be present to trigger  
this rule, otherwise it thinks something like /posts/johnny/2 is  
asking for action johnny with id 2.

# anything like /posts/johnny/2 should is a show action. i.e., show

page 2 of johnny’s posts.

map.connect ‘:controller/:id/:page’,  
:requirements =\> { :page =\> /\d+/},  
:action =\> ‘show’,  
:page =\> nil

Note: here the page CAN be nil, so it’ll generate the shortest URL,  
i.e., /posts/johnny instead of /posts/johnny/1

these are RESTful routes I’m using, which you can modify to suit.

---

<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, 8:53pm UTC](https://www.ruby-forum.com/t/how-to-make-will-paginate-generated-url-search-engine-friendly/121820/3 "2007-12-01T20:53:57Z")

</div>

sorry, I just found a problem with my scheme. it cannot handle

/:controller/:page

(action index, page 2)

when controller is something other than my default controller. In  
fact I don’t think there’s a way to distinguish between

/controller/page and /controller/id unless your id has some unique  
pattern other than \d+

I’ll let someone more knowledgable chime in 🙂

---

<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 3, 2007, 5:51am UTC](https://www.ruby-forum.com/t/how-to-make-will-paginate-generated-url-search-engine-friendly/121820/4 "2007-12-03T05:51:21Z")

</div>

Thanks for your reply 🙂
