drenda
drenda

Reputation: 6244

Apache 2.4 url rewriting with https

I'm trying to do an url rewriting with Apache 2.4. I want that requests to

  1. http://subdomain.domain.com
  2. http://www.subdomain.domain.com
  3. https://www.subdomain.domain.com

are remapped to

https://subdomain.domain.com

to avoid an error in SSL wildcard cert that doesn't not match www.subdomain.domain.com.

I tried with:

    <VirtualHost ip:80>
        ServerName subdomain.domain.com
        RewriteEngine On
        RewriteCond %{HTTPS} off
        RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}       
   </VirtualHost>
   <VirtualHost ip:80>
        ServerName www.subdomain.domain.com
        Redirect permanent / https://subdomain.domain.com
   </VirtualHost>
   <VirtualHost ip:443>
        ServerName www.subdomain.domain.com
        Redirect permanent / https://subdomain.domain.com
   </VirtualHost>
  <VirtualHost ip:443>
        ServerName subdomain.domain.com
        ...
        ...
        ...

My configuration works for (1) and (2) but not for (3). Where is my mistake?

Upvotes: 0

Views: 1651

Answers (1)

Chris Lear
Chris Lear

Reputation: 6742

I think the problem is that one of your port 443 virtualhosts does not have SSL on.

Try this

<VirtualHost ip:443>
    ServerName www.subdomain.domain.com
    Redirect permanent / https://subdomain.domain.com
    SSLEngine on
    SSLCertificateFile    /something
    SSLCertificateKeyFile /something
</VirtualHost>

Otherwise, the request simply won't be understood, because it's encrypted.

See eg How to redirect https to http without any SSL Certificate for why this is necessary.

Upvotes: 1

Related Questions