乘客(Ruby on Rails)中的301从根域重定向到www子域? [英] 301 redirect in Passenger (Ruby on Rails) from root domain to www sub domain?

查看:58
本文介绍了乘客(Ruby on Rails)中的301从根域重定向到www子域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can you create a permanent redirect (301) in Passenger? 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.

有什么线索吗?

谢谢!

推荐答案

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

Server-level redirects are done with the HTTP server, not the Application server. Here's some examples:

阿帕奇

<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 设置,但如果您可以选择在此处执行此操作,我建议这样做.

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. 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.

这篇关于乘客(Ruby on Rails)中的301从根域重定向到www子域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆