2016-02-17 61 views
0

如何将http页面重定向到https?Angularjs重定向到https

我已经在login.config中尝试了这个代码块,但它转到http并导航到https。我想先解析https。

resolve: { 
     data: function ($q, $state, $timeout, $location, $window) { 
      var deferred = $q.defer(); 

       if ($location.protocol() !== 'https') { 
        $window.location.href = $location.absUrl().replace('http', 'https'); 
       } 
       deferred.resolve(true); 


      return deferred.promise; 
     } 
    } 

感谢

回答

1

你应该使用Web服务器重定向到HTTPS。

例如,如果你使用nginx的编辑nginx.conf

server { 
     listen   80; 
     server_name my.domain.com; 
     return   301 https://$server_name$request_uri; 
} 

server { 
     listen   443 ssl; 
     server_name my.domain.com; 
     # add Strict-Transport-Security to prevent man in the middle attacks 
     add_header Strict-Transport-Security "max-age=31536000"; 

     [....] 
} 
1

我相信你在错误的层面解决问题您的服务器部分。您的AngularJS项目托管在某种服务器上(NodeJS,Apache HTTP,NGINX等)。这台服务器应该照顾重做!

对于Apache HTTP,你可能想看一看:https://wiki.apache.org/httpd/RedirectSSL

在NGINX的情况下,你将有类似的东西在你的配置:

server { 
    listen 80 default_server; 
    listen [::]:80 default_server; 
    server_name _; 
    return 301 https://$host$request_uri; 
}