简体   繁体   English

Apache从https重定向

[英]Apache redirect from https

I installed an SSL Certificate on my server, and https works fine. 我在服务器上安装了SSL证书,并且https工作正常。

Http redirect works fine for example http://login.example.com -> https://example1.com/index.html Http重定向工作正常,例如http://login.example.com > https://example1.com/index.html

But redirect from https not working, for example https://login.example.com -> https://example1.com/index.html . 但是无法从https重定向,例如https://login.example.com > https://example1.com/index.html

its just go to the main page of my server https://redirect.example.com 它只是转到我服务器的主页https://redirect.example.com

Any suggestions on how to get the redirect from https to work? 关于如何从https重定向进行工作的任何建议?

Thanks ! 谢谢 !

<VirtualHost *:80>
    RewriteEngine On
    ServerName redirect.example.com
    RewriteCond %{HTTP_HOST} ^login\.example\.com(.*)
    RewriteRule (.*) https://example1.com/index.html [L]
</VirtualHost>

<VirtualHost *:443>
    SSLEngine on
    SSLCertificateFile    /etc/httpd/my.crt
    SSLCertificateKeyFile /etc/httpd/my.pem
    ServerName redirect.mydomain.com

    RewriteEngine On
    ServerName redirect.example.com
    RewriteCond %{HTTP_HOST} ^login\.example\.com(.*)
    RewriteRule (.*) https://example1.com/index.html [L]

</VirtualHost>

Thanks! 谢谢!

I see that you have a duplicate ServerName directive in your SSL host declaration. 我看到您的SSL主机声明中有重复的ServerName指令。 That could be causing trouble. 那可能会造成麻烦。

From the Apache documentation : Apache文档中

If you are using name-based virtual hosts, the ServerName inside a section specifies what hostname must appear in the request's Host: header to match this virtual host. 如果使用的是基于名称的虚拟主机,则部分中的ServerName指定必须在请求的Host:标头中显示的主机名,以匹配该虚拟主机。

What exactly do you mean by multiple rules ? 您所说的多个规则到底是什么意思? Multiple domain names/subdomains ? 多个域名/子域?

A plain redirect of http(s)://login.example.com to https://example1.com/index.html should be achieved this way: 应该通过以下方式将http(s)://login.example.com重定向到https://example1.com/index.html

<VirtualHost *:80>
ServerName login.example.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^login\.example\.com
RewriteRule (.*) https://example1.com/index.html [L]
</VirtualHost>

<VirtualHost *:443>
SSLEngine on
SSLCertificateFile    /etc/httpd/my.crt
SSLCertificateKeyFile /etc/httpd/my.pem
ServerName login.example.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^login\.example\.com
RewriteRule (.*) https://example1.com/index.html [L]
</VirtualHost>

Important: ServerName should match your SSL certificate's Common Name (unless it's a wildcard certificate). 重要提示:ServerName应该与SSL证书的公用名匹配(除非它是通配符证书)。

NB: RewriteEngine may be overkill for your purpose since the Apache mod_alias module provides the Redirect directive which may be sufficient here. 注意:由于Apache mod_alias模块提供了Redirect指令,因此RewriteEngine对于您的目的可能是过大的。 From the Apache documentation: When not to use mod_rewrite 来自Apache文档: 何时不使用mod_rewrite

Use server alias for your Rewrite Rule: 使用服务器别名作为重写规则:

ServerAlias login.example.com

you can use multiple ServerAlias for multiple Rewrite Rule 您可以将多个ServerAlias用于多个Rewrite Rule

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

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