我正在一个仅拥有monsite.fr的SSL证书的网站上工作。我正在尝试删除www。并重定向到https://monsite.fr
如果用户键入www.monsite.fr,则重定向将不起作用,他将不会重定向到https://monsite.fr,而是重定向到https://www.monsite.fr并获得认证错误NET::ERR_CERT_COMMON_NAME_INVALID

这是mysite.conf文件的内容:

<IfVersion < 2.3 >
  NameVirtualHost *:80
  NameVirtualHost *:443
</IfVersion>

<VirtualHost *:80>
  ServerName monsite.fr
  Redirect / https://monsite.fr/

  #RewriteEngine On
  #RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
  #RewriteRule ^(.*)$ https://%1$1 [R=permanent,L]
</VirtualHost>

<VirtualHost *:80>
  ServerName www.monsite.fr
  Redirect / https://monsite.fr/
</VirtualHost>

<VirtualHost *:443>
  ServerName monsite.fr
  ServerAlias www.monsite.fr

  #RewriteEngine On
  #RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
  #RewriteRule ^(.*)$ https://%1$1 [R=permanent,L]

  RewriteEngine On
  RewriteCond %{HTTPS} =on
  RewriteCond %{HTTP_HOST} ^www\.
  RewriteRule ^(.*)$ https://monsite.fr/$1 [R,QSA,L]

  DocumentRoot "/opt/monsite.fr/htdocs"
</VirtualHost>


有解决方案将www.monsite.fr重定向到https://monsite.fr吗?

最佳答案

修改您的虚拟主机配置,如下所示:

  <VirtualHost *:80>
      ServerName www.monsite.fr
      ServerAlias monsite.fr
      RedirectMatch ^/(.*)$ https://monsite.fr/$1
    </VirtualHost>

    <VirtualHost *:443>
      ServerName www.monsite.fr
      RedirectMatch ^/(.*)$ https://monsite.fr/$1
    </VirtualHost>

    <VirtualHost *:443>
      ServerName monsite.fr
      SSLEngine On
      SSLCertificateFile    /path_to_cert/server.crt
      SSLCertificateKeyFile /path_to_key/server.key
      DocumentRoot "/opt/monsite.fr/htdocs"
    </VirtualHost>


第一个虚拟主机块会将所有请求从http://www.monsite.fr重定向到https://monsite.fr。它还会将所有请求从http://monsite.fr重定向到https://monsite.fr

第二个虚拟主机块会将所有请求从https://www.monsite.fr重定向到https://monsite.fr

第三个虚拟主机块将为https://monsite.fr提供内容。确保编辑上述配置,并为SSL证书添加正确的路径/path_to_cert/server.crt,为私钥添加/path_to_key/server.key

08-27 21:45