13

We have a client server hosting our web application using Apache 2.2 & Tomcat 6 in RHEL. I have setup apache re-write rule for http to https redirection and it works fine. We have two DNS names that are used to access the same application. Test1.com and Test2.com. I want all the users trying to access http:// test1.com or https:// test1.com to https:// test2.com. As mentioned, http:// test1.com to https:// test2.com redirection is working fine. I am not able to implement https://test1.com to https://test2.com.

I have tried Virtual Hosts, ServerAlias, NameVirtualHost, but nothing works. Any suggestions if we can handles this via re-write would help. Any other pointers that might lead to the resolution of this issue will be appreciated.

Thanks

2
  • How are you currently doing the redirection for http://test1.com to https://test2.com?
    – clmarquart
    May 4, 2011 at 12:39
  • I am using Rewrite rule as follows: RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https:// test2.com$1 [R=301]
    – Noman Amir
    May 4, 2011 at 13:24

4 Answers 4

11

I solved this issue with redirect, but I had to setup virtual host for https redirect with all necessary ssl settings.

<VirtualHost *:80>
    ServerName test1.com
    Redirect "/" "https://test2.com/"
</VirtualHost>

<VirtualHost *:443>
    ServerName test1.com
    Redirect "/" "https://test2.com/"
    SSLEngine on
    # SSLProxyEngine On
    SSLCertificateFile /path/site.crt
    SSLCertificateKeyFile /path/site.key
    SSLCertificateChainFile /path/DigiCertCA.crt
    SSLProtocol ALL -SSLv2 -SSLv3
    SSLHonorCipherOrder on
    SSLCipherSuite EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH
</VirtualHost>

<VirtualHost *:443>
    ServerName test2.com
    ...
    SSLEngine on
    # SSLProxyEngine On
    SSLCertificateFile /path/site.crt
    SSLCertificateKeyFile /path/site.key
    SSLCertificateChainFile /path/DigiCertCA.crt
    SSLProtocol ALL -SSLv2 -SSLv3
    SSLHonorCipherOrder on
    SSLCipherSuite EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH
</VirtualHost>
3
  • 2
    Please mark this answer as true. It absolutely works.
    – weiglt
    Sep 2, 2017 at 6:35
  • 1
    This is correct answer. In my opinion you can't redirect https without a valid certificate for both sites with simple Apach Jun 7, 2018 at 14:06
  • While I agree that this is true, it's not applicable in all cases. In my case, we are no longer utilising site2.com and want any legitimate traffic to be redirected to site1.com. We don't want to have to maintain a certificate for site2.com just for a redirect to work.
    – Sera H
    Jul 25, 2018 at 10:05
10

Try the following:

 RewriteEngine On 

 RewriteCond %{HTTP_HOST} test1.com$
 RewriteRule ^(.*)$ https://test2.com$1 [L,NC,R=301]

If you have a <VirualHost> for both :80 and :443, this redirect should go in both configurations.

3
  • Thx for the suggestion, but it didn't work. We have ssl cert for 2nd domain but not for 1st one.So when i access 1st domain using https, it throws security warning.I want to redirect to 2nd domain regardless of http or https.
    – Noman Amir
    May 4, 2011 at 14:02
  • 2
    @Noman Amir: That is AFAIK not possible because the redirection is done on HTTP layer whereas SSL works on the layer below. Therefore before the redirect can be performed the SSL layer must have established a connection - and this results in the security warning.
    – Robert
    May 4, 2011 at 15:06
  • @Robert: Even though it is disappointing, really thanks for the info!Is there a possibility that I can redirect after the security warning?
    – Noman Amir
    May 4, 2011 at 15:22
0

I had site1 with https (certificate) and site2 with http (without certificate), both on the same IP (virtual hosts. Then I noteiced that site2 was getting incorrectly indexed by Google for https, using site1's content.

Whilst for RewriteCond listening

  • for port 80, the redirect needs to sit in the .htaccess for site2,
  • for port 443, the redirect needs to sit in the .htaccess for site1.

But then the discrimination no longer goes by port but by HTTP_HOST (the DNS name).

For me, site1 = shop.smartgart.com, site2 = one0.com. I put this into site1's .htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^shop.smartgart.com$
RewriteRule ^(.*)$ https://shop.smartgart.com/$1 [R=301,L]

That is: If the HTTP_HOST being handled is not site1, then redirect to site1, using the supplied suffix ($1).

Works for me!

-1

I solved this issue with MULTIPLE redirects, not the same as @A Kunin 's answer. Because I use different certificates for both site, and it will report certificate error if I just redirect from httpS://test1.com to httpS://test2.com.

My solution is: httpS://test1.com --> http://test1.com --> httpS://test2.com

<VirtualHost *:80>
    ServerName test1.com
    Redirect "/" "https://test2.com/"
</VirtualHost>

<VirtualHost *:443>
    ServerName test1.com

    #### The Tricky ####
    Redirect "/" "http://test2.com/"

    SSLEngine on
    # SSLProxyEngine On
    SSLCertificateFile /path/site1.crt
    SSLCertificateKeyFile /path/site1.key
    SSLCertificateChainFile /path/DigiCertCA1.crt
    SSLProtocol ALL -SSLv2 -SSLv3
    SSLHonorCipherOrder on
    SSLCipherSuite EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH
</VirtualHost>

<VirtualHost *:443>
    ServerName test2.com
    ...
    SSLEngine on
    # SSLProxyEngine On
    SSLCertificateFile /path/site2.crt
    SSLCertificateKeyFile /path/site2.key
    SSLCertificateChainFile /path/DigiCertCA2.crt
    SSLProtocol ALL -SSLv2 -SSLv3
    SSLHonorCipherOrder on
    SSLCipherSuite EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH
</VirtualHost>
1
  • The step in between is completely useless, it only disrupts the encrypted requests. And the worst thing, the user doesn't even notice his connection was not encrypted for one step.
    – Aviv Lo
    Mar 29, 2021 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.