在Apache上从www重定向到非www

在Apache上从www重定向到非www,apache,Apache,我在将我的站点重定向到非www上时遇到问题。我的RPi上实际上有NextCloudPlus,但我想使用我的RPi托管一些其他简单的站点。 几乎所有的都看起来不错,但当我进入时,我会被重定向到www而不是非www curl-I /已启用站点/000-default.conf <VirtualHost _default_:80> DocumentRoot /var/www/nextcloud <IfModule mod_rewrite.c> RewriteEn

我在将我的站点重定向到非www上时遇到问题。我的RPi上实际上有NextCloudPlus,但我想使用我的RPi托管一些其他简单的站点。 几乎所有的都看起来不错,但当我进入时,我会被重定向到www而不是非www

curl-I

/已启用站点/000-default.conf

<VirtualHost _default_:80>
  DocumentRoot /var/www/nextcloud
  <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
  </IfModule>
</VirtualHost>
<VirtualHost *:80>
  DocumentRoot /var/www/examplecom
  ServerName examplecom
  <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
  </IfModule>
</VirtualHost>

DocumentRoot/var/www/nextcloud
重新启动发动机
重写cond%{HTTPS}=在…上
重写规则^/?(*)https://%{SERVER_NAME}/$1[R,L]
DocumentRoot/var/www/examplecom
服务器名示例COM
重新启动发动机
重写cond%{HTTPS}关闭[或]
重写cond%{HTTP_HOST}^www\。[北卡罗来纳州]
重写cond%{HTTP_HOST}^(?:www\)?(.+)$[NC]
重写规则^https://%1%{REQUEST_URI}[L,NE,R=301]
/已启用站点/examplecom.conf

<IfModule mod_ssl.c>
  <VirtualHost _default_:443>
    SSLEngine on
      SSLCertificateFile /etc/letsencrypt/live/examplecom/fullchain.pem
      SSLCertificateKeyFile /etc/letsencrypt/live/examplecom/privkey.pem
  </VirtualHost>
</IfModule>

斯伦金安
SSLCertificateFile/etc/letsencrypt/live/examplecom/fullchain.pem
SSLCertificateKeyFile/etc/letsencrypt/live/examplecom/privkey.pem

我做错了什么?

如果你要求
http://www.example.com
,Apache捕捉到这一点以转到默认的VirtualHost,因为它位于端口80上。在这里您重定向到
https://%{SERVER\u NAME}
,这里SERVER\u NAME==www.example.com。删除默认的VirtualHost,您就会没事了

另外,ServerName应该是您期望的虚拟主机的域名。我想说:

ServerName example.com
ServerAlias www.example.com
ServerName example.com
ServerAlias www.example.com