Apache2将https:// www重定向到https:// non-www

时间:2019-06-11 16:21:41

标签: apache redirect apache2 no-www

我想将import numpy as np import matplotlib.pyplot as plt for i in range(5): x, y, _ = np.loadtxt('File%s.csv' %i, unpack=True) plt.plot(x, y, label='File %s' %i) plt.legend() plt.show() 重定向到https://www.mydom.com

我有一个http://mydom.com的{​​{1}}

mydom.conf

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> mydom.conf重定向到http://mydom.com。很好。

我的问题出在http://www.mydom.com,它没有重定向到https://mydom.com,而是显示了https://www.mydom.com的内容

如何将https://mydom.com重定向到https://mydom.com

2 个答案:

答案 0 :(得分:1)

使用vi命令在您的网站的public_html文件夹或文档根目录中只需创建一个.htaccess文件。在您的.htacess文件中添加以下代码。

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

如果public_html文件夹中已有一个.htaccess文件,请尝试使用以下命令进行备份:

$ mv .htaccess .htaccess.bak

$ cp .htaccess .htaccess.bak

希望这会有所帮助。

答案 1 :(得分:1)

您可以使用多个虚拟主机块

<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>

您唯一需要确保的是证书对example.comwww.example.com均有效,这样您就不会在重定向之前收到https异常。