简体   繁体   English

如何以角度从 HTTP 重定向到 HTTPS

[英]How to redirect from HTTP to HTTPS in angular

I have the following .httaccess for redirect to HTTPS from HTTP page, but it apparently doesn't work as expected:我有以下.httaccess用于从 HTTP 页面重定向到 HTTPS,但它显然没有按预期工作:

RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} (www\.)?myuniversity.ro
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html

The problem is, when I type my actual website like:问题是,当我输入我的实际网站时:
http://www.myuniversity.ro , it automatically goes to http://www.myuniversity.ro/home , like how it supposed to do, but after a refresh is going to HTTPS, is there a way to go into HTTPS from the first attempt? http://www.myuniversity.ro ,它会自动转到http://www.myuniversity.ro/home ,就像它应该做的那样,但是刷新后转到 HTTPS,有没有办法进入 HTTPS从第一次尝试开始?
I mean, to go from http://www.myuniversity.ro directly to https://www.myuniversity.ro/home without being necessary a refresh?我的意思是,从http://www.myuniversity.ro直接转到https://www.myuniversity.ro/home而不需要刷新?
This is my route configuration:这是我的路由配置:

const routes: Routes = [
  { path: '',   redirectTo: '/home', pathMatch: 'full' },
  { path: 'home', component: HomeComponent }
];

You should not handle http to https redirects in angular.您不应该以角度处理 http 到 https 的重定向。 This makes your server not more secure.这使您的服务器不再安全。 Because the api is not using angular, so assets etc.因为 api 没有使用 angular,所以资产等。

Could you please check if this works for you你能检查一下这对你有用吗

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} www\.domain\.de [NC]
RewriteRule ^(.*)$ https://www\.domain\.de%{REQUEST_URI} [R=301,L]

And.和。 Why using a redirect in angular to home and not using the '' path to show home component.为什么使用重定向到 home 的角度而不是使用 '' 路径来显示 home 组件。 Just think about it.考虑一下。 I guess it is better to have the domain in the search engines instead of this 'home' thing.我想最好在搜索引擎中拥有域而不是这个“家”的东西。

# If an existing asset or directory is requested go to it as it is RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR] RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d RewriteRule ^ - [L] RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} (www\\.)?myuniversity.ro RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

You have these 2 rules in the wrong order.您以错误的顺序拥有这 2 条规则。 The HTTP to HTTPS redirect should be first. HTTP 到 HTTPS 重定向应该是第一个。 The first rule prevents further processing when the requested URL maps to a file or directory.当请求的 URL 映射到文件或目录时,第一条规则阻止进一步处理。 Requesting the root (ie. http://www.myuniversity.ro ) is a directory .请求根(即http://www.myuniversity.ro )是一个目录 In this case, the HTTP to HTTPS redirect in .htaccess is not even processed.在这种情况下,甚至不会处理.htaccess的 HTTP 到 HTTPS 重定向。

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

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