Score:1

Website root trailing slash auto-removed (issue)

jp flag

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.

kz flag
Please include the contents of your `.htaccess` file in your question, not as an external link. Also include the relevant examples, otherwise, the question as posted is not a question.
kz flag
"I have SSL redirect forced" - What does this mean? Where is this? It does not appear to be in the linked `.htaccess` file. In fact, there doesn't appear to be any "redirects" in the code you've posted? From your examples it looks like it could be the HTTP to HTTPS redirect that is the issue (ie. the "SSL forced redirect" - whatever that is).
jp flag
@MrWhite, with "SSL redirect forced" I mean a feature in the CMS that permits to use SSL for all the website pages. You're right, in that .htaccess there is no SSL redirect for all http requests. Do you think it could be the issue? How should I add it into my .htaccess?
Score:0
kz flag

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.

jp flag
Thank you so much for the help and for explaining. I tried to add the lines you wrote above but nothing is changed. The rules were executed because I can see the HTTP procotol is changed with HTTPS but the website root slash has been removed however. At this point, should be a CMS configuration problem, right?
kz flag
@simone_meh But if you request the HTTPS URL directly it works OK! This would imply you have a malformed HTTP to HTTPS in the server config itself (that executes before `.htaccess`)? Do you have access to the server config?
jp flag
sure, I am on a linux VPS. ``` <VirtualHost *:80> ServerName www.example.com Redirect permanent / https://www.example.com </VirtualHost> ``` The missing trailing slash on redirect permanent could be the problem?
kz flag
@simone_meh That looks like an error in the server redirect (unless something is missing in the comment). You are missing a trailing slash on the target URL!? I've updated my answer.
jp flag
Solved! Thank you so much!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.