Apache – Fix Website Root Trailing Slash Auto-Removed Issue

.htaccessapache-2.4mod-rewrite

as per title, when I enter my website URL manually (examples at the end of the topic), the trailing slash at the end of the domain being removed automatically.
I have SSL redirect forced and Apache ModRewrite enabled in .htaccess and this is the file content (as you can see, it's autogenerated from Prestashop CMS (1.7.x)):

    # ~~start~~ Do not remove this comment, Prestashop will keep automatically the code outside this comment when .htaccess will be generated again
# .htaccess automaticaly generated by PrestaShop e-commerce open-source solution
# http://www.prestashop.com - http://www.prestashop.com/forums
 
<IfModule mod_rewrite.c>
<IfModule mod_env.c>
SetEnv HTTP_MOD_REWRITE On
</IfModule>
 
RewriteEngine on
 
 
#Domain: www.example.com
RewriteRule . - [E=REWRITEBASE:/]
RewriteRule ^api(?:/(.*))?$ %{ENV:REWRITEBASE}webservice/dispatcher.php?url=$1 [QSA,L]
 
# Images
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$1$2$3.jpg [L]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$1$2$3$4.jpg [L]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$1$2$3$4$5.jpg [L]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$1$2$3$4$5$6.jpg [L]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6$7.jpg [L]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7$8.jpg [L]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8$9.jpg [L]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9$10.jpg [L]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^c/([0-9]+)(\-[\.*_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/c/$1$2$3.jpg [L]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^c/([a-zA-Z_-]+)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/c/$1$2.jpg [L]
# AlphaImageLoader for IE and fancybox
RewriteRule ^images_ie/?([^/]+)\.(jpe?g|png|gif)$ js/jquery/plugins/fancybox/images/$1.$2 [L]
 
# Dispatcher
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ %{ENV:REWRITEBASE}index.php [NC,L]
</IfModule>
 
AddType application/vnd.ms-fontobject .eot
AddType font/ttf .ttf
AddType font/otf .otf
AddType application/font-woff .woff
AddType font/woff2 .woff2
<IfModule mod_headers.c>
<FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|svg)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
</IfModule>
 
#If rewrite mod isn't enabled
ErrorDocument 404 /index.php?controller=404
 
# ~~end~~ Do not remove this comment, Prestashop will keep automatically the code outside this comment when .htaccess will be generated again

Examples:

 - www.example.com/contacts -> https://www.example.comcontacts [WRONG]   
 - example.com/contacts -> https://www.example.comcontacts [WRONG]
 - http://example.com/contacts -> https://www.example.comcontacts [WRONG]
   
 - https://example.com/contacts -> https://www.example.com/contacts [OK]
 - https://www.example.com/contacts -> https://www.example.com/contacts [OK]

Any help is appreciated.

Best Answer

As mentioned in comments, there is nothing in the config you have posted that would cause this. There are no redirects at all in the config you posted. This is most probably a configuration error in your CMS (Prestashop).

The problem appears to be with the HTTP to HTTPS redirect that is removing the slash at the start of the URL-path (immediately after the hostname).

However, you may be able to "workaround" this issue by implementing an HTTP to HTTPS (and non-www to www) redirect in .htaccess - which occurs before your CMS is able to do anything. So, your CMS does not need to redirect the request.

For example, assuming you have a reasonably standard server, where the SSL is managed by the application server (no SSL proxy like Cloudflare etc.) then you could do something like the following at the very top of the .htaccess file, before the # ~~start~~ comment.

# Redirect non-www to www + HTTPS
RewriteCond %{HTTP_HOST} ^(example\.com) [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]

# Redirect HTTP to HTTPS (remaining requests)
RewriteCond %{HTTPS} =off
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

NB: Test first with 302 (temporary) redirects to avoid potential caching issues.


UPDATE:

I tried to add the lines you wrote above but nothing is changed.

This would imply you have a malformed HTTP to HTTPS in the server config itself (that executes before .htaccess)

<VirtualHost *:80>
ServerName www.example.com
Redirect permanent / https://www.example.com
</VirtualHost>

If this is your actual config, then you are missing the trailing slash on the target URL! This will result in the malformed (missing slash after the domain) redirect you are seeing.

It should be:

Redirect permanent / https://www.example.com/

You will need to make sure the browser cache is clear before testing, since the erroneous redirect will have been cached.