2016-04-06 4 views
0

をリダイレクトhttps://example.comからhttps://www.example.comへの要求をリダイレクトするためにRailsのコード上で私のルビーです:Rubyはここ

class ApplicationController < ActionController::Base 
before_filter :add_www_subdomain 

private 

    def add_www_subdomain 
     if Rails.env.production? 
     unless /^www/.match(request.host) 
      redirect_to("#{request.protocol}www.#{request.host_with_port}",status: 301) 
     end 
     end 
    end 
end 

誰かがhttps://example.com/product/abcで着地時に今の問題は、彼らはおそらくhttps://www.example.comにリダイレクトされています、彼らはhttps://www.example.com/product/abcに行く必要があります。これにはどんなトリックがありますか?ありがとう

答えて

2
redirect_to("#{request.protocol}www.#{request.host_with_port}#{request.fullpath}",status: 301) 

この種類のリダイレクトは、Webサーバーにより適していると思います。これらのルールである:

アパッチ

<VirtualHost *:80> 
    ServerName example.com 
    Redirect permanent/http://www.example.com/ 
</VirtualHost> 

source

nginxの

server { 
    listen 80; 
    server_name www.domain.com; 
    # $scheme will get the http protocol 
    # and 301 is best practice for tablet, phone, desktop and seo 
    return 301 $scheme://domain.com$request_uri; 

}

server { 
    listen 80; 
    server_name domain.com; 
    # here goes the rest of your config file 
    # example 
    location/{ 

     rewrite ^/cp/login?$ /cp/login.php last; 
     # etc etc... 

    } 
} 

source

関連する問題