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?
views:
128answers:
7You can do it with rails routing or with mod_rewrite in Apache.
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
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.
A combination of Apache's mod_rewrite and some regex should be all you need.
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]+/
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.
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