301将Passenger(Ruby on Rails)从根域重定向到www子域?

时间:2011-02-07 18:38:00

标签: redirect passenger

如何在Passenger中创建永久重定向(301)?其他地方有关于如何在Rails中执行重定向的帖子,但在服务器级别而不是在Rails级别进行重定向似乎更好。

任何线索?

谢谢!

2 个答案:

答案 0 :(得分:2)

服务器级重定向是通过HTTP服务器完成的,而不是应用程序服务器。以下是一些例子:

<强>的Apache

<VirtualHost xxx.xxx.xxx.xxx:80>
    ServerAlias example.com
    Redirect Permanent / http://www.example.com
</VirtualHost>

<强> Nginx的

server {
  server_name example.com;
  rewrite ^/(.*) http://www.example.com/$1 permanent;
}

<强> Lighttpd的

$HTTP["host"] =~ "^example\.com$" {
  url.redirect = ( "^/(.*)" => "http://www.example.com/$1" )
}

虽然技术上可以在堆栈中实现这一点,例如使用Rack应用程序,但最有效的做法是尽早保存服务器cpu周期。有时您必须稍后执行此操作,例如使用像Heroku这样的主机不允许您更改HTTP设置,但如果您可以选择在此处执行此操作,那么我建议使用。

答案 1 :(得分:0)

你确定你想要它在Passenger级而不是Nginx / Apache级别......也就是说,为什么重定向甚至会在堆栈中走得那么远。

根据您使用的服务器,网上有资源可以告诉您如何完成此操作。