Heroku 301 Redirect

送分小仙女□ 提交于 2019-12-06 05:09:01

问题


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)
]
} 

回答1:


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




回答2:


You have to add a new free Custom Domain.



来源:https://stackoverflow.com/questions/8802431/heroku-301-redirect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!