Search code examples
apachesslhttp-redirectshopwareno-www

apache2 www to non-www redirection not working


After trying many different combinations to redirect my shopware6 installation on a server with apache2, I am not able to make the www to non-www redirection work.

Here's my conf file :

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

Here are the different ways I tried to make the redirection (added this below "RewriteEngine on" in the conf file) :

RewriteCond %{HTTP_HOST} ^www.vanparysbakery.emakers.be [NC]
RewriteRule ^(.*)$ http://vanparysbakery.emakers.be/$1 [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]

Also tried this in the .htaccess file of my shopware folder :

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

&&&&

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

What am I doing wrong? I learned many things about this and all the solutions I found did not give the expected result. I suspect an apache configuration to "block" this redirection.

Any kind of help would be great :)

UPDATE (new content of my conf file as suggested by @MikeMoy) :

<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/$1 [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/$1 [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>

UPDATE 2 : I tried the same fix for another website on which we have the same behaviour 'covarmed.emakers.be'. After what I am executing this command : curl -I www.covarmed.emakers.be (I also tried this with "covarmed.emakers.be" & "http://www.covarmed.emakers.be"). The response was everytime the same :

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

But in browsers, the result is still the same, the redirection is NOT working.CURL RESPONSE


Solution

  • something like this, then restart your server for new config to be loaded

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