简体   繁体   中英

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 ?

Simply create a .htaccess file using vi command in the public_html folder or document root of your website. Add the code below in your .htacess file.

RewriteCond %{HTTP_HOST} ^www.mydom.com$ RewriteCond %{SERVER_PORT} ^443 RewriteRule ^(.*)$ https://mydom.com/$1 [R=301]

If you have an existing .htaccess file in the public_html folder, try to make a backup using the command below:

$ mv .htaccess .htaccess.bak

or

$ cp .htaccess .htaccess.bak

Hope this helps.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM