Heroku 301重定向 [英] Heroku 301 Redirect

查看:132
本文介绍了Heroku 301重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使 http://domain.com 301重定向到 http://www.domain.com ?我习惯使用.htaccess来ModRewrite它,但是我知道我不能在Heroku上这样做。



.htaccess的示例:

 选项+ FollowSymlinks 
RewriteEngine在

RewriteCond%{HTTP_HOST} ^ domain.com [NC]
RewriteRule ^(。*)$ http://www.domain.com/$1 [R = 301,L]

我的文件结构:

   -  / public 
--- / style
--- index.html
- config.ru

我只是提供单个页面和我的config.ru包含:

 使用Rack :: Static,
:url = > [/ style],
:root => public

运行lambda {| env |
[
200,
{
'Content-Type'=> 'text / html',
'Cache-Control'=> 'public,max-age = 86400'
},
File.open('public / index.html',File :: RDONLY)
]
}


解决方案

使用rack_rewrite(gem'rack-rewrite'一个rack_rewrite.rb在你的初始化程序目录中;

$ $ p $ YourAppName :: Application.config.middleware.insert_before(Rack :: Lock, Rack :: Rewrite)do
r301%r {。*},'http://www.yourdomainname.com$&',:if => Proc.new {| rack_env |
rack_env ['SERVER_NAME']!='www.yourdomainname.com'
}
end if Rails.env =='production'

这就是说,如果被请求的服务器名称不是www.yourdomainname.com,那么将其重定向到www.yourdomainname.com。


How do I make it so http://domain.com 301 redirects to http://www.domain.com? I'm used to using .htaccess to ModRewrite it, but I've learned I can't do that on Heroku.

Example of .htaccess:

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

My file structure:

- /public
--- /style
--- index.html
- config.ru

I'm just serving up the single page, and my config.ru consists of this:

use Rack::Static,
:urls => ["/style"],
:root => "public"

run lambda { |env|
[
200,
{
'Content-Type' => 'text/html',
'Cache-Control' => 'public, max-age=86400'
},
File.open('public/index.html', File::RDONLY)
]
} 

解决方案

use rack_rewrite (gem 'rack-rewrite' in your Gemfile) and create a rack_rewrite.rb in your initializers directory with;

YourAppName::Application.config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
  r301 %r{.*}, 'http://www.yourdomainname.com$&', :if => Proc.new {|rack_env|
    rack_env['SERVER_NAME'] != 'www.yourdomainname.com'
  }
end if Rails.env == 'production'

this says, if the servername being requested is not www.yourdomainname.com then redirect it to www.yourdomainname.com

这篇关于Heroku 301重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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