Heroku 301 重定向
如何使 http://domain.com 301 重定向到 http://www.domain.com?我习惯使用 .htaccess 来 ModRewrite 它,但我知道我不能在 Heroku 上这样做。
.htaccess 示例:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
我的文件结构:
- /public
--- /style
--- index.html
- config.ru
我只是提供单个页面,我的 config.ru 包含以下内容:
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)
]
}
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)
]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用rack_rewrite(Gemfile中的gem'rack-rewrite')并在初始化目录中创建一个rack_rewrite.rb;
这表示,如果请求的服务器名不是 www.yourdomainname.com,则将其重定向到 www.yourdomainname.com
use rack_rewrite (gem 'rack-rewrite' in your Gemfile) and create a rack_rewrite.rb in your initializers directory with;
this says, if the servername being requested is not www.yourdomainname.com then redirect it to www.yourdomainname.com
您必须添加新的免费自定义域。
You have to add a new free Custom Domain.