简体   繁体   中英

Apache redirect from https

I installed an SSL Certificate on my server, and https works fine.

Http redirect works fine for example 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 .

its just go to the main page of my server https://redirect.example.com

Any suggestions on how to get the redirect from https to work?

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. That could be causing trouble.

From the Apache documentation :

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.

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:

<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).

NB: RewriteEngine may be overkill for your purpose since the Apache mod_alias module provides the Redirect directive which may be sufficient here. From the Apache documentation: When not to use mod_rewrite

Use server alias for your Rewrite Rule:

ServerAlias login.example.com

you can use multiple ServerAlias for multiple Rewrite Rule

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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