HTTP + HTTPS + www +非www Apache配置

时间:2018-06-05 21:20:42

标签: apache http https virtualhost no-www

在我的Apache配置中,所有内容都被重定向到HTTPS(这很好)。但https://www.example.comhttps://example.com仍然存在。

问题:如何只有https://www.example.com而不是非www?

我应该使用301 Redirection还是其他技术?

如何更改此配置:

<VirtualHost *:80>
  ServerName example.com
  ServerAlias *.example.com
  RewriteEngine on
  RewriteCond %{HTTPS} !on
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>

<IfModule mod_ssl.c>
<VirtualHost *:443>
  ServerName example.com
  ServerAlias *.example.com
  DocumentRoot /home/www/example
  <Directory />
    Options FollowSymLinks
    AllowOverride All
    Order deny,allow
    Allow from all
    Require all granted
  </Directory>
  SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
  Include /etc/letsencrypt/options-ssl-apache.conf
  SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
</VirtualHost>
</IfModule>

2 个答案:

答案 0 :(得分:1)

Relymcd's answer解决了这个问题,但它也需要证书行存在(如果不存在则会失败):

<VirtualHost *:443>
    ServerName example.com
    Redirect 301 / https://www.example.com/
    SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
</VirtualHost>

答案 1 :(得分:0)

一种方法是更改​​当前虚拟主机private void txtRed_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { if (e.KeyCode == Keys.Tab) { txtRed.BackColor = System.Drawing.Color.Red; txtYellow.BackColor = System.Drawing.Color.DarkGray; txtGreen.BackColor = System.Drawing.Color.DarkGray; } } 并为非www添加新的虚拟主机

ServerName www.example.com