本文介绍了github网址样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让网站中的用户拥有自己的网址,例如(类似于GitHub,例如我的账户是http:// github.com/sr3d)。这将有助于搜索引擎优化,因为每个配置文件都在相同的域下,与子域方法相同。



我的网站运行在Rails和Nginx / Passenger上。目前我有一个解决方案,使用nginx.conf文件中的一堆重写,以及硬编码的控制器名称(也支持命名空间)。如果你们想看看,我可以在这里分享包括nginx.conf。



我想知道是否有更好的方法让网址变成这样。



(如果你建议一个更好的地方发表这个问题,然后请让我知道)



干杯,

Alex

解决方案

将这行放在routes.rb文件的末尾,(这样就不会干扰其他控制器路由) / p>

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

在users_controller中,使用以下行来获取用户

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

我认为这不需要任何nginx魔术或url重写。

HTH


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

解决方案

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

这篇关于github网址样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 14:50