Prestashop, attachments and pretty URLs


I am using Prestashop 1.6 with pretty URLs and recently encountered an error (status code 404) when trying to access attachments to a product. The URL was of the form /de/index.php?controller=attachment?id_attachment=42. The link is generated using Links::getPageLink() in the following line in product.tpl:

<h4><a href=“{$link->getPageLink('attachment', true, NULL, "id_attachment={$attachment.id_attachment}”)|escape:‘html’:‘

Interestingly, the 404 page is not the one generated by Prestashop, but the one returned by nginx itself. That means that no Prestashop code was executed when handling this request. Instead, nginx is looking for a file with the path de/index.php, which doesn’t exist, because /de is not a physical folder. When manually removing the language part (/de) from the URL, everything works.

So how do we fix this? Editing one of the classes (Links or Dispatcher) would be a mistake, because the risk of accidentally breaking something that works now is simply too high. In any case, the problem has to be solved by the PrestaShop developers.

The solution for now is to add the following lines to the nginx configuration:

   location ~ /de/index.php {
     rewrite ^/de/index\.php$ /index.php redirect;
location ~ /fr/index.php {
     rewrite ^/fr/index\.php$ /index.php redirect;

This works because the URL for index.php should not have a language prefix anyway. Once an update of Prestashop fixes the problem, these lines will no longer have an effect and I will remove them.

Permalink: /blog/prestashop-attachments-pretty-urls.html