views:

68

answers:

1

Hi everyboy,

I used to have PHP websites and using url rewriting on picture to have SEO friendly urls, On php I had links like /image/blablablabla-1234-blablabla rewriting to: /image/1234.jpg by using a url rewrite rule on apache .htaccess file.

So I would like the structure /image/<text>-ID-<text> to return /image/<id>.jpg reguardless the text content.

Now I totally switched to ruby on rails and I would like to know what is the best way to do that on RoR? I am hosting websites on apache with passenger.

I guess the solution is in the rails route file but I can't figure out the appropriate way to achieve this.

Thanks in advance.

A: 
  1. If you need to pass through the rails controller (for authentication, etc), you may:

A. Try the friendly_id plugin. You have this functionality out of the box.

friendly_id guide

B. Add a before_filter in your controller (to parse any kind of URL in that form)

before_filter :rewrite_id

def rewrite_id
  if params[:id] && params[:id] =~ /[a-zA-Z0-9]*-([0-9]*)-[a-zA-Z0-9]*/
    params[:id] = $1.to_i
  end
end
  1. If this is just static data, it is better to be server directly by the frontend apache, so you should use the same technique as with php.
Vlad Zloteanu
Thanks for answering. I am using already friendly id for articles with fixed comments.In my situation I want to have a rewriting rule working with whatever the text 'blablabla' is.
SilverRuby
Updated my answer.
Vlad Zloteanu
Thank very much.I think the B solution is the best way since each website might have different rules. I will give it a shot
SilverRuby
The B method is working as expected. Thanks a lot.I had it working on a video controller and using paths /video/show/<text>-1-<text> processed as /video/show/1But with pictures the method is not working.I have pictures stored in the path public/images/gallery/ of the rails application.And by willing to rewrite the common /images/gallery/1.jpg by /images/gallery/first-1-image the method fails.I created a images controller but the rewrite rule isn't working.Am I missing something?
SilverRuby
The files in the /public folder are not served by Rails stack, but directly by Passenger. After all, it seems you will still need mod_rewrite :)
Vlad Zloteanu
Thanks for the hint. The rewrite method in the controller will be useful to SEOize some stuffs. Merci Monsieur
SilverRuby
:) Je vous en prie.
Vlad Zloteanu