[英]Redirect http://domain.com/subpage to http://www.domain.com/subpage in Rails
I assume this is not really a difficult issue but I am using so many different solutions and don't really know what it best. 我认为这并不是一个真正困难的问题,但是我正在使用许多不同的解决方案,并且真的不知道这是什么。
I am using Ruby on Rails and have my apps on Heroku and I want to 301 redirect everything on my naked domain (@) to my www-domain. 我正在使用Ruby on Rails,并且我的应用程序在Heroku上,并且我想301将裸域(@)上的所有内容重定向到我的www域。 Eg
例如
http://domain.com --> http://www.domain.com http://domain.com/subpage --> http://www.domain.com/subpage http : //domain.com-> http://www.domain.com http://domain.com/subpage-> http://www.domain.com/subpage
Right now I am handling this with my DNS by first deleting both @ and www entries. 现在,我正在通过首先删除@和www条目来处理DNS。 Then I set a redirect of the entire website to http://www.domain.com (which re-creates the DNS entries for both @ and www).
然后,我将整个网站重定向到http://www.domain.com (这将为@和www重新创建DNS条目)。 Lastly I change my www DNS-entry to CNAME and the name of the heroku-app (http://myapp.herokuapp.com).
最后,我将我的www DNS条目更改为CNAME,并将其名称改为heroku-app(http://myapp.herokuapp.com)。
This seems to be forwarding http://domain.com/subpage to http://www.domain.com (without the subpage). 这似乎是将http://domain.com/子页面转发到http://www.domain.com (不带子页面)。
What I am looking for now is the proper/recommended way to handle this in a simple/elegant way. 我现在正在寻找以简单/优雅的方式处理此问题的正确/建议的方法。
DNS? DNS? Routes?
路线? .htaccess?
.htaccess? (if so, how do I alter .htaccess in RubyOnRails)
(如果是这样,如何在RubyOnRails中更改.htaccess)
Thanks in advance! 提前致谢!
These questions may be related to your question, maybe have a look at those. 这些 问题可能与您的问题有关,也可以看看这些问题。 It seems (from what I have read), that heroku does not allow access to anything like .htaccess or anything, so it seems like Rack middleware would be the best option.
从我所读的内容看来,heroku不允许访问.htaccess之类的东西,因此,似乎Rack中间件将是最佳选择。
Where is your domain registered? 您的域名在哪里注册? If with GoDaddy, they offer a service that will handle the 301 redirect for you, but it requires that you sign up for one of their hosting plans.
如果使用GoDaddy,他们将提供可为您处理301重定向的服务,但它要求您注册其托管计划之一。 The lowest cost will do (~$5 per month).
费用最低(每月约5美元)。 Not free, but painless and requires no coding, etc.
不是免费的,但是很轻松,不需要编码等。
(notice : did not try anything of this, but it should work) (注意:没有尝试任何这种方法,但是应该可以使用)
One might argue that the nice way is to use the DNS or your webservers capabilities to do that. 有人可能会认为,一种不错的方法是使用DNS或您的网络服务器功能。 However, it is possible to do it with rails if you need it.
但是,如果需要,也可以使用滑轨进行操作。
All in all, it has the advantage that you will easily keep any params / path in the process, as you told you wanted. 总而言之,它的优点是您可以轻松地在过程中保留您所希望的任何参数/路径。 Plus, the logic is inside the app and won't be lost if you need to scale up / change domain name.
另外,该逻辑位于应用程序内部,如果您需要扩展/更改域名,则不会丢失。 On the bad side, your full stack will be hit anytime someone uses the bare domain name.
不利的一面是,只要有人使用裸域名,您的全部筹码就会被击中。
you can try a before_filter
in your application controller : 您可以在应用程序控制器中尝试使用
before_filter
:
before_filter :redirect_to_www
def redirect_to_www
redirect_to subdomain: 'www' unless request.subdomain == 'www'
end
if you want to avoid a "magic redirection" and make it clear for everyone, on rails 3 you can do this directly in the routes: 如果您想避免“魔术重定向”并使每个人都清楚,则可以在3号轨道上直接在路线中执行此操作:
constraints ->(request){ request.subdomain != 'www' } do
match '(*all)' => redirect( subdomain: 'www' )
end
you should also add the subdomain to your default url options (application controller): 您还应该将子域添加到默认的url选项(应用程序控制器):
def default_url_options( option = {} )
{subdomain: 'www'}
end
In the end what I had to do was to point the @-DNS to my normal web host (since @ demands an IP and Heroku will only accept CNAME) and the www-DNS to Heroku. 最后,我要做的就是将@ -DNS指向我的普通Web主机(因为@要求IP,并且Heroku将只接受CNAME),而将www-DNS指向Heroku。
At my normal webhost I put a .htaccess that redirected all traffic there to my www-domain like this: 在我的普通网络主机上,我放置了一个.htaccess,它将所有流量重定向到我的www域,如下所示:
RewriteEngine On
### re-direct to www
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.