github url style

≯℡__Kan透↙ 提交于 2019-12-12 01:32:42

问题


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:


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



来源:https://stackoverflow.com/questions/2968722/github-url-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!