apachehttp-redirectapache2no-www

Apache2 redirect https://www to https://non-www


I want to redirect https://www.mydom.com to http://mydom.com

I have an mydom.conf into apache2/sites-available/

mydom.conf:

#HTTP(www/non-www) -> HTTPS(non-www)
<VirtualHost *:80>

   ServerName mydom.com
   ServerAlias www.mydom.com

   Redirect permanent / https://mydom.com/

</VirtualHost>

#ACTIVATE HTTPS AND REVERSE PROXY -> test-1.0.0
<VirtualHost _default_:443>

   SSLEngine On

   SSLCertificateFile /opt/ssl/new/mydom_com.crt
   SSLCertificateKeyFile /opt/ssl/new/mydom_com.key
   SSLCertificateChainFile /opt/ssl/new/mydom_com.ca-bundle

   BrowserMatch ".*MSIE.*" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0
   SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL

   ServerName mydom.com
   ServerAlias www.mydom.com

   ProxyRequests Off
   ProxyPreserveHost On
   ProxyPass / http://127.0.0.1:8080/test-1.0.0/
   ProxyPassReverse / http://127.0.0.1:8080/test-1.0.0/

</VirtualHost>

With this mydom.conf It works partial.

Domains http://mydom.com and http://www.mydom.com are redirected to https://mydom.com . This is good.

My problem is at https://www.mydom.com it is not redirected to https://mydom.com, but show the content from https://mydom.com

How I can redirect https://www.mydom.com to https://mydom.com?


Solution

  • You can use multiple vhosts block

    <VirtualHost _default_:443>
    
       SSLEngine On
    
       SSLCertificateFile /opt/ssl/new/mydom_com.crt
       SSLCertificateKeyFile /opt/ssl/new/mydom_com.key
       SSLCertificateChainFile /opt/ssl/new/mydom_com.ca-bundle
    
       BrowserMatch ".*MSIE.*" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0
       SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
    
       ServerName mydom.com
    
       ProxyRequests Off
       ProxyPreserveHost On
       ProxyPass / http://127.0.0.1:8080/test-1.0.0/
       ProxyPassReverse / http://127.0.0.1:8080/test-1.0.0/
    
    </VirtualHost>
    
    <VirtualHost _default_:443>
    
       SSLEngine On
    
       SSLCertificateFile /opt/ssl/new/mydom_com.crt
       SSLCertificateKeyFile /opt/ssl/new/mydom_com.key
       SSLCertificateChainFile /opt/ssl/new/mydom_com.ca-bundle
    
       BrowserMatch ".*MSIE.*" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0
       SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
    
       ServerName www.mydom.com
    
       Redirect permanent / https://mydom.com/
    </VirtualHost>
    

    The only thing you need make sure is that certificate is valid for both example.com and www.example.com, so that you don't get https exception before the redirect.