Berlin
Berlin

Reputation: 1464

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!

Upvotes: 0

Views: 70

Answers (3)

user6882728
user6882728

Reputation:

Use server alias for your Rewrite Rule:

ServerAlias login.example.com

you can use multiple ServerAlias for multiple Rewrite Rule

Upvotes: 0

Kate
Kate

Reputation: 1836

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

Upvotes: 0

Kate
Kate

Reputation: 1836

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.

Upvotes: 1

Related Questions