github url style

2019-08-30 02:18发布

I wanted to have users within my website to have their own URL like http://mysite.com/username (similar to GitHub, e.g. my account is http:// github. com/sr3d). This would help with SEO since every profile is under the same domain, as apposed to the sub-domain approach.

My site is running on Rails and Nginx/Passenger. Currently I have a solution using a bunch of rewrite in the nginx.conf file, and hard-coded controller names (with namespace support as well). I can share include the nginx.conf here if you guys want to take a look.

I wanted to know if there's a better way of making the URL pretty like that.

(If you suggest a better place to post this question then please let me know)

Cheers,

Alex

1条回答
趁早两清
2楼-- · 2019-08-30 02:43

Place this line right at the end of the routes.rb file, ( So that this doesn't interfere with other controller routes )

map.connect "/:username", :controller=> "users", :action => "show"

in users_controller, use the following line to fetch the user

  @user = User.find_by_username(params[:username]) 

I don't think this would require any nginx magic or url rewrites.

HTH

查看更多
登录 后发表回答