开发者

How to rewrite image/<text>-<imageID>-<text> to image/ID.jpg?

开发者 https://www.devze.com 2022-12-27 06:43 出处:网络
I used to have PHP websites and using url rewriting on picture to have SEO friendly urls开发者_如何转开发,

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.


  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.
0

精彩评论

暂无评论...
验证码 换一张
取 消