.htaccess redirecting non SSL Domain to another SSL Domain

I have the following .htaccess:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.old_domain.de$
RewriteRule (.*) https://new_domain.de/$1 [R=301,L]

The Problem is, the rule seem to only redirect www.old_domain.de or http://www.old_domain.de correctly, but does not redirect https://old_domain.de or old_domain.de (Which jumps to https:// automaticaly). I the second case I get the following error:

SSL_ERROR_UNRECOGNIZED_NAME_ALERT

I already tried to rewrite https:// and non-www domains into http://www. but wasn't able to achieve the wanted results somehow. Probably I miss something Important here.

Is it possible there's something wrong with the https:// forcing at the .htaccess of my new domain?

1 answer

  • answered 2018-10-18 13:06 Ankit Rastogi

    For redirecting http://www.old_domain.de or www.old_domain.de or http://old_domain.de or old_domain.de use the below code

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(www\.)?old_domain\.de$ [NC]
    RewriteRule (.*) https://new_domain.de/$1 [R=301,L]
    

    Your code is missing the (www\.)? in RewriteCond which will make the rewrite works irrespective of whether it contains a www or not.

    You can test the same using htaccess tester at link

    Similarly you should have a separate virtual host configured for SSL (HTTPS). In that virtual host you also needs to copy the above code.

    SSL is configured on port 443 to listen, so search for something similar to below code in your htaccess

    <VirtualHost *:443>
    
        ServerName www.old_domain.de
        ServerAlias old_domain.de
    
        SSLEngine on
        SSLCertificateFile /etc/apache2/ssl/example/apache.crt
        SSLCertificateKeyFile /etc/apache2/ssl/example/apache.key 
    
     </VirtualHost>
    

    If you are still unable to find the file where virtual host listening on 443 is configured you can run the apache with the -S flag like below command

    httpd -S or apachectl -S
    

    This will give you the name of the file where virtual host listening on 443 is configured as in the below output

    *:80     default server xxx.xxx.xxx.xxx (/etc/httpd/conf.d/eco.conf:1)
             port 80 namevhost xxx.xxx.xxx.xxx (/etc/httpd/conf.d/eco.conf:1)
    
    *:443    default server xxx.xxx.xxx.xxx (/etc/httpd/conf.d/ssl.conf:1)
             port 443 namevhost xxx.xxx.xxx.xxx (/etc/httpd/conf.d/ssl.conf:1)
    

    For example /etc/httpd/conf.d/ssl.conf is the file in which virtual host listening on 443 is configured.