2016-04-06 6 views
0

Hier mein Ruby-Umleitung ist auf Rails-Code eine Anfrage https://example.com-https://www.example.com zu umleiten:Rubin in Rails www Sub-Domain

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 

Nun ist die Frage, wenn jemand an https://example.com/product/abc landet sie angeblich zu https://www.example.com umgeleitet werden, sie sollten zu https://www.example.com/product/abc gehen. Gibt es einen Trick dafür? Danke

Antwort

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

Ich denke, diese Art von Redirect ist besser für den Webserver geeignet. Dies sind die Regeln:

Apache

<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

Verwandte Themen