问题
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