简体   繁体   English

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

[英]301 redirect in Passenger (Ruby on Rails) from root domain to www sub domain?

How can you create a permanent redirect (301) in Passenger? 如何在Passenger中创建永久重定向(301)? There are posts elsewhere on how to perform the redirect in Rails, but it seems better to do the redirect at the server level rather than at the Rails level. 其他地方也有关于如何在Rails中执行重定向的文章,但是在服务器级别而不是在Rails级别执行重定向似乎更好。

Any clues? 有线索吗?

Thanks! 谢谢!

Server-level redirects are done with the HTTP server, not the Application server. 服务器级重定向是通过HTTP服务器而不是应用程序服务器完成的。 Here's some examples: 以下是一些例子:

Apache 阿帕奇

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

Nginx Nginx的

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

Lighttpd Lighttpd的

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

While it's technically possible to achieve this later in the stack, like with a Rack app, it makes the most sense to do this as early as possible to save your server cpu cycles. 虽然技术上可以在堆栈中实现这一点,例如使用Rack应用程序,但最有效的做法是尽早保存服务器cpu周期。 Sometimes you have to do this later, for instance with a host like Heroku that won't let you change your HTTP settings, but if you have the option to do it here that's what I recommend. 有时您必须稍后执行此操作,例如使用像Heroku这样的主机不会让您更改HTTP设置,但如果您可以选择在此处执行此操作,那么我建议您这样做。

Are you sure you want it at the Passenger level and not at the Nginx/Apache level... ie, why have the redirect even get that far down the stack. 你确定你想要它在Passenger级别而不是Nginx / Apache级别......也就是说,为什么重定向甚至可以在堆栈中走得那么远。

Depending on what server you are using, there are resources on the net that tell you how do accomplish this. 根据您使用的服务器,网上有资源告诉您如何完成此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM