apache2 www 到非 www 重定向不起作用

apache2 www to non-www redirection not working

在尝试了许多不同的组合以使用 apache2 将我的 shopware6 安装重定向到服务器后,我无法使 www 到非 www 重定向工作。

这是我的配置文件:

<VirtualHost *:80>
        ServerName vanparysbakery.emakers.be
        ServerAlias www.vanparysbakery.emakers.be
        RewriteCond %{HTTPS} off
        RewriteEngine On
        ServerSignature Off
</VirtualHost>
<VirtualHost *:443>
        Protocols h2 h2c http/1.1
        LoadModule ssl_module /usr/lib64/apache2-prefork/mod_ssl.so
        DocumentRoot /var/www/bakery/public
        ServerName vanparysbakery.emakers.be
        ServerAlias www.vanparysbakery.emakers.be
        <Directory /var/www/bakery/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Require all granted
        </Directory>
        SSLEngine on
        SSLProtocol all -SSLv2 -SSLv3
        SSLCipherSuite HIGH:!aNULL:!MD5:!ADH:!DH:!RC4
        SSLHonorCipherOrder on
        Include /etc/letsencrypt/options-ssl-apache.conf
        SSLCertificateFile /etc/letsencrypt/live/vanparysbakery.emakers.be/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/vanparysbakery.emakers.be/privkey.pem
</VirtualHost>

以下是我尝试进行重定向的不同方式(在 conf 文件中的“RewriteEngine on”下方添加):

RewriteCond %{HTTP_HOST} ^www.vanparysbakery.emakers.be [NC]
RewriteRule ^(.*)$ http://vanparysbakery.emakers.be/ [L,R=301]

&&&&

RewriteCond %{SERVER_NAME} =www.vanparysbakery.emakers.be [OR]
RewriteCond %{SERVER_NAME} =vanparysbakery.emakers.be
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

还在我的 shopware 文件夹的 .htaccess 文件中尝试了这个:

RewriteEngine On
RewriteRule ^(.*) http://vanparysbakery.emakers.be/ [QSA,L,R=301]

&&&&

RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/ [R=301,L]

我做错了什么?我学到了很多关于这个的东西,我找到的所有解决方案都没有给出预期的结果。我怀疑 apache 配置会“阻止”此重定向。

任何形式的帮助都会很棒 :)

更新(@MikeMoy 建议的我的 conf 文件的新内容):

<VirtualHost *:80>
        ServerName vanparysbakery.emakers.be
        ServerAlias www.vanparysbakery.emakers.be
        RewriteEngine on
        #Redirect    301 /   http://vanparysbakery.emakers.be/
        RewriteCond %{HTTP_HOST} www.vanparysbakery.emakers.be [NC]
        RewriteRule ^/?(.) vanparysbakery.emakers.be/ [L,R,NE]
        ServerSignature Off
</VirtualHost>
<VirtualHost *:443>
        Protocols h2 h2c http/1.1
        LoadModule ssl_module /usr/lib64/apache2-prefork/mod_ssl.so
        DocumentRoot /var/www/bakery/public
        ServerName vanparysbakery.emakers.be
        ServerAlias www.vanparysbakery.emakers.be
        <Directory /var/www/bakery/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Require all granted
        </Directory>
        RewriteEngine on
        RewriteCond %{HTTP_HOST} www.vanparysbakery.emakers.be [NC]
        RewriteRule ^/?(.) https://vanparysbakery.emakers.be/ [L,R,NE]
        SSLEngine on
        SSLProtocol all -SSLv2 -SSLv3
        SSLCipherSuite HIGH:!aNULL:!MD5:!ADH:!DH:!RC4
        SSLHonorCipherOrder on
        Include /etc/letsencrypt/options-ssl-apache.conf
        SSLCertificateFile /etc/letsencrypt/live/vanparysbakery.emakers.be/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/vanparysbakery.emakers.be/privkey.pem
</VirtualHost>

更新 2: 我对我们有相同行为的另一个网站尝试了相同的修复 'covarmed.emakers.be'。在我执行此命令之后:curl -I www.covarmed.emakers.be(我也尝试使用“covarmed.emakers.be”和“http://www.covarmed.emakers.be”)。响应每次都相同:

HTTP/1.1 302 Found
Date: Fri, 27 Aug 2021 09:42:09 GMT
Server: Apache/2.4.38 (Debian)
Location: https://covarmed.emakers.be//
Content-Type: text/html; charset=iso-8859-1

但在浏览器中,结果仍然相同,重定向不起作用。

像这样,然后重新启动服务器以加载新配置

<VirtualHost *:80>
        RewriteRule ^(.*)$ https://vanparysbakery.emakers.be/ [R,L]
</VirtualHost>


<VirtualHost *:443>
        RewriteCond %{HTTP_HOST} www.vanparysbakery.emakers.be [NC]
        RewriteRule ^/?(.*) https://vanparysbakery.emakers.be/ [L,R,NE]
 </VirtualHost>