简体   繁体   English

使用Angular和Apache无法从http重定向到https

[英]Redirect from http to https using Angular and Apache not working

I have an SSL cert installed and I'd like to redirect all traffic from http to https. 我已经安装了SSL证书,并且希望将所有流量从http重定向到https。 I've added this section to my Apache server: 我已将此部分添加到我的Apache服务器中:

IncludeOptional conf.d/*.conf
<VirtualHost *:80>
    ServerName www.memoriesgameseries.com
    Redirect "/" "https://www.memoriesgameseries.com/"
</VirtualHost>
Listen 443
<VirtualHost *:443>
    ServerName www.memoriesgameseries.com
    SSLEngine on
    SSLCertificateFile "/etc/httpd/memoriesgameseries_com.crt"
    SSLCACertificateFile "/etc/httpd/memoriesgameseries_com.ca-bundle"
    SSLCertificateKeyFile "/etc/httpd/memoriesgameseries.com.key"
</VirtualHost>

As per the Apache documentation. 根据Apache文档。 My Angular routing file looks like this: 我的Angular路由文件如下所示:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { MemoriesComponent }  from '../memories/memories.component';
import { TearsComponent }  from '../tears/tears.component';

const routes: Routes = [
  { path: '', redirectTo: 'memories', pathMatch: 'full' },
  { path: 'memories', component: MemoriesComponent },
  { path: 'tears', component: TearsComponent }
]

@NgModule({
  imports: [ RouterModule.forRoot(routes)],
  exports: [ RouterModule ]
})

export class RoutingModule {};

However, whenever I attempt to access the website via http, instead of correctly redirecting, when I type in a root address, such as www.memoriesgameseries.com and http://memoriesgameseries the redirect fails to kick in at all. 但是,无论何时我尝试通过http访问网站,而不是正确重定向,当我键入根地址(例如www.memoriesgameseries.comhttp://memoriesgameseries ,重定向都根本无法启动。 When I type in a URL above the root domain, such as http://memoriesgameseries.com/memories the redirect kicks in and I see the https URL in the browser (eg https://www.memoriesgameseries.com/memories ), but I get a: 当我在根域上方输入URL时,例如http://memoriesgameseries.com/memories ,重定向就会启动,并且我在浏览器中看到https URL(例如https://www.memoriesgameseries.com/memories ),但我得到:

Not Found 未找到

The requested URL /memories was not found on this server. 在此服务器上找不到请求的URL /内存。

Response from the server. 服务器的响应。 What's likely to be causing this issue? 是什么可能导致此问题?

The point is that server are trying to manage 404. That's no life, let SPA do that for you. 关键是服务器正在尝试管理404。这是没有生命的,让SPA为您完成。

When 404 redirect to index.html 404重定向到index.html时

  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]

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

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

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