Search code examples
apache.htaccessurlhttp-redirectvhosts

Slash being removed from url when redirecting from http to https


For some reason when I visit one of these urls in non https format on any of their pages, it will redirect you to a broken link, where a slash has been removed (thus changing the actual domain in the url and taking users off-site).

http://www.example.com/fr-FR does a 302 redirect to https://www.example.comfr-fr

At the top level, things are working fine and http://www.example.com redirects to https://www.example.com

I have tried editing the .htaccess file to catch all http and redirect to https, but no luck. I have tried to redirect specific urls to their https counterparts, but we still hit that same 302 redirect before my redirects are hit.

I've tried many different redirects in the vhost file as well, but for some reason nothing seems to affect this issue. I've also tried deleting the .htaccess and gutting the vhost file to remove any culpable code, but that didn't help either. Does anyone have any idea what could be causing this issue?

Here is the .conf file:

    <VirtualHost *:80>
    ServerName example.com/
    ServerAlias www.example.com/
    DocumentRoot /var/www/2lbgfz7l.example.com
    DirectoryIndex index.html
    ErrorLog /var/log/apache2/2lbgfz7l.example.com.error.log
    CustomLog /var/log/apache2/2lbgfz7l.example.com.access.log combined
    Redirect 301 / https://www.example.com/$1

    <Location / >
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} -s [OR]
        RewriteCond %{REQUEST_FILENAME} -l [OR]
        RewriteCond %{REQUEST_FILENAME} -d
        RewriteRule ^.*$ - [NC,L]
        RewriteRule ^(.*) /index.html/ [NC,L]
    </Location>

    <Directory "/var/www/2lbgfz7l.example.com">
            Options -Indexes +FollowSymLinks -MultiViews
            AllowOverride All
    </Directory>
</VirtualHost>

<IfModule mod_ssl.c>
  <VirtualHost *:443>
        ServerName example.com/
        ServerAlias www.example.com/
        DocumentRoot /var/www/2lbgfz7l.example.com
        DirectoryIndex index.html

        ErrorLog /var/log/apache2/example.com.error.log
        CustomLog /var/log/apache2/example.com.access.log combined

        <Directory "/var/www/2lbgfz7l.example.com">
                Options -Indexes +FollowSymLinks -MultiViews
                AllowOverride All
        </Directory>

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        SSLEngine on

        <FilesMatch "\.(cgi|shtml|phtml|php)$">
            SSLOptions +StdEnvVars
        </FilesMatch>
        <Directory /usr/lib/cgi-bin>
            SSLOptions +StdEnvVars
        </Directory>

        BrowserMatch   "MSIE [2-6]" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0
        BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
        SSLCertificateChainFile /etc/apache2/ssl.crt/example.com.chain-2.pem
        SSLCertificateFile /etc/apache2/ssl.crt/example.com.chain-2.pem
        SSLCertificateKeyFile /etc/apache2/ssl.crt/example.com.key
    </VirtualHost>
</IfModule>

Here is the .htaccess file:

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access 1 year"
    ExpiresByType image/jpeg "access 1 year"
    ExpiresByType image/gif "access 1 year"
    ExpiresByType image/png "access 1 year"
    ExpiresByType text/css "access 1 month"
    ExpiresByType text/html "access 1 month"
    ExpiresByType application/pdf "access 1 month"
    ExpiresByType text/x-javascript "access 1 month"
    ExpiresByType text/css "access plus 1 year"
    ExpiresByType application/x-shockwave-flash "access 1 month"
    ExpiresByType image/x-icon "access 1 year"
    ExpiresDefault "access 1 month"
</IfModule>

<IfModule mod_headers.c>
  <FilesMatch "\.(js|css|xml|gz)$">
    Header append Vary: Accept-Encoding
  </FilesMatch>
</IfModule>

<ifModule mod_gzip.c>
    mod_gzip_on Yes
    mod_gzip_dechunk Yes
    mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
    mod_gzip_item_include handler ^cgi-script$
    mod_gzip_item_include mime ^text/.*
    mod_gzip_item_include mime ^application/x-javascript.*
    mod_gzip_item_exclude mime ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

<IfModule mod_deflate.c>
    SetOutputFilter DEFLATE
    AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml text/x-js text/js
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^example\.com [NC]
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

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

Solution

  • Looks like the issue was that the .conf file I was working on in /etc/apache2/sites-available was not being read by the server, because in /etc/apache2/sites-enabled instead of a symlink to the .conf file I was working on in /sites-available there was a duplicate of the .conf file, which was being read by the server. This conf file held the incorrect redirect - once I deleted this duplicate file and added a symlink by running

    sudo ln -s /etc/apache2/sites-available/example.com.conf from the etc/apache2/sites-enabled directory, my updated .conf file was read.

    In my updated file I had removed the directive:

       <Location / >
            RewriteEngine on
            RewriteCond %{REQUEST_FILENAME} -s [OR]
            RewriteCond %{REQUEST_FILENAME} -l [OR]
            RewriteCond %{REQUEST_FILENAME} -d
            RewriteRule ^.*$ - [NC,L]
            RewriteRule ^(.*) /index.html/ [NC,L]
        </Location>
    

    and after removing this the issue resolved.