简体   繁体   English

AngularJS重定向到https

[英]Angularjs redirect to https

How can i redirect http page to https ? 如何将http页面重定向到https?

I have tried this code block in login.config, but it goes to http and navigate to https. 我已经在login.config中尝试过此代码块,但是它转到http并导航到https。 I want to resolve https firstly. 我想先解析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;
        }
    }

Thanks 谢谢

You should redirect to https using webserver. 您应该使用网络服务器重定向到https。

For example, if you're using nginx edit your server section of nginx.conf 例如,如果您使用的是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"; 

       [....]
}

I believe you are tackling the problem on the wrong level. 我相信您正在错误地解决问题。 Your AngularJS project is hosted on some kind of server (NodeJS, Apache HTTP, NGINX, whatever). 您的AngularJS项目托管在某种服务器(NodeJS,Apache HTTP,NGINX等)上。 This server should take care of doing the redirect! 该服务器应注意进行重定向!

For Apache HTTP, you might want to take a look at: https://wiki.apache.org/httpd/RedirectSSL 对于Apache HTTP,您可能需要看一下: https : //wiki.apache.org/httpd/RedirectSSL

In case of NGINX, you would have something like that in your config: 对于NGINX,您的配置中将包含以下内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM