1

www以外をwwwに書き換えることができる必要がありますが、(www以外の)サブドメインが存在する場合はそうではありません。

したがって、example.comから-> www.example.comになりますが、sub.example.comはsub.example.comのままです。

私はRails3にいます。これは、Rackミドルウェアを使用して実行する必要があるようですが、問題はこれがマルチテナントアプリであるため、TLDが任意のドメインになる可能性があることです。

これは私が今のところいるところです:

  Class Www

  def initialize(app)
    @app = app
  end

  def call(env)

    request = Rack::Request.new(env)

    if !request.host.starts_with?("www.")
      [301, {"Location" => request.url.sub("//","//www.")}, self]
    else
      @app.call(env)
    end

  end

  def each(&block)
  end

end

任意のポインタをいただければ幸いです。

4

1 に答える 1

1

The code you have right now will rewrite "sub.example.com", your call function could be rewritten like this :

def call(env)
  request = Rack::Request.new(env)

  # Redirect only if the host is a naked TLD
  if request.host =~ /^[^.]+\.[^.]+$/
    [301, {"Location" => request.url.sub("//","//www.")}, self]
  else
    @app.call(env)
  end
end
于 2012-09-09T10:48:54.993 に答える