views:

128

answers:

7

how do you make it so that www.site.com/123 or www.site.com/123/some-headline www.site.com/123/anything-at-all will lead users to the same place which is www.site.com/123 ? I think the routing in Ruby on Rails can do it. But other than that, what other methods can do that. can it be done by Apache alone?

+3  A: 

You can do it with rails routing or with mod_rewrite in Apache.

http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html

Corban Brook
A: 

You could easily write a Servlet that just sends a redirect to the original site. I'm sure Apache could do this also.

Servlet Example:

public void handleRequest(HttpServletRequest request, HttpServletResponse response) {
  response.sendRedirect("http://www.site.com/123");
}

web.xml

<servlet-mapping>
   <servlet-name>RedirectServlet</servlet-name>
   <url-pattern>/*</url-pattern>
</servlet-mapping>

And your servlet would live under the 123 directory in webapps

Gandalf
A: 

You're talking about slugs. The http://site.com/123/ is the url and the some-headline bit are slugs.

You will have to set your routing somehow so the server knows this.

IainMH
+1  A: 

A combination of Apache's mod_rewrite and some regex should be all you need.

mod_rewrite

turkeyburger
+2  A: 

Using Mod Rewrite:

RewriteEngine ON 
RewriteRule ^([0-9]+)/*$ /$1 [R=301,L]

Using routes.rb

map.connect ':my_id/:headline', :controller => 'controller_name', 
            :action => 'action_name', :my_id => /[0-9]+/
adnan.
A: 

Although it does not answer exactly your question, most rails sites use a slug after a dash rather than a slash, just like this: www.site.com/123-my-headline.

You do this by making your own to_params method in the model.

No more work needs to be done because the find method will return the page #123 automatically since the ruby to_i method will return the integer that starts the string and ignore all characters starting with the first non-numerical character.

allesklar
A: 

As of rails 1.2.6 you can add both routes:

map.connect ':id', :controller=>:post,:action=>:show
map.connect ':id/:title', :controller=>:post,:action=>:show

The RESTful thing may vary

Hope it helps you

Fer