Heroku 301 重定向

发布于 2024-12-25 19:10:18 字数 773 浏览 1 评论 0原文

如何使 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我是有多爱你 2025-01-01 19:10:18

使用rack_rewrite(Gemfile中的gem'rack-rewrite')并在初始化目录中创建一个rack_rewrite.rb;

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

这表示,如果请求的服务器名不是 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;

YourAppName::Application.config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
  r301 %r{.*}, 'http://www.yourdomainname.com
amp;', :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

等风来 2025-01-01 19:10:18

您必须添加新的免费自定义域

You have to add a new free Custom Domain.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文