Nginx rewrite rules for Prestashop 1.4 and 1.5

Introduction

This article is for users of Prestashop 1.4 or 1.5 who are struggling to get their shop working with nginx web server.

For Prestashop 1.6 users I reccomend the settings found in my other article: /configure-rewrite-rules-for-nginx

I am going to assume that your nginx configurations are located in a location of;
/etc/nginx

If this is not the case then adjust the locations accordingly.

Step 1

I am also going to assume you might want to use this config for different websites on the same server, so the best way to manage this is create a config file that can be shared between websites (or server definition blocks as they are known):

Create a folder

mkdir -p /etc/nginx/shopapps.d (the -p just means create any parent folders if required)

Then you need to copy the following configs into a file on the server, this could be done using nano editor (and copy/paste in your console) or an ftp or similar program such as winscp (for Windows) or fetch for Apple Mac.

Note: there are a lot of rewrite rules for the older Prestashop versions ;-)


nano /etc/nginx/shopapps.d/prestashop-v14.conf


        # new for 1.5.x

        rewrite ^/api/?(.*)$ /webservice/dispatcher.php?url=$1 last;

        rewrite "^/c/([0-9]+)(\-[_a-zA-Z0-9-]*)/(.*)\.jpg$" /img/c/$1$2.jpg last;
        rewrite "^/c/([_a-zA-Z-]+)/(.*)\.jpg$" /img/c/$1.jpg last;

        rewrite "^/([a-z0-9]+)\-([a-z0-9]+)(\-[_a-zA-Z0-9-]*)/(\P{M}\p{M}*)*\.jpg$" /img/p/$1-$2$3.jpg last;
        rewrite "^/([0-9]+)\-([0-9]+)/(\P{M}\p{M}*)*\.jpg$" /img/p/$1-$2.jpg last;
        rewrite "^/([0-9])(\-[_a-zA-Z0-9-]*)?/(\P{M}\p{M}*)*\.jpg$" /img/p/$1/$1$2.jpg last;
        rewrite "^/([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/(\P{M}\p{M}*)*\.jpg$" /img/p/$1/$2/$1$2$3.jpg last;
        rewrite "^/([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/(\P{M}\p{M}*)*\.jpg$" /img/p/$1/$2/$3/$1$2$3$4.jpg last;
        rewrite "^/([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/(\P{M}\p{M}*)*\.jpg$" /img/p/$1/$2/$3/$4/$1$2$3$4$5.jpg last;
        rewrite "^/([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/(\P{M}\p{M}*)*\.jpg$" /img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6.jpg last;
        rewrite "^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/(\P{M}\p{M}*)*\.jpg$" /img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7.jpg last;
        rewrite "^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/(\P{M}\p{M}*)*\.jpg$" /img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8.jpg last;
        rewrite "^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/(\P{M}\p{M}*)*\.jpg$" /img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9.jpg last;

        rewrite "^/([0-9]+)\-(\P{M}\p{M}*)+\.html(.*)$" /index.php?controller=product&id_product=$1$3 last;
        rewrite "^/([0-9]+)\-([a-zA-Z0-9-]*)(.*)$" /index.php?controller=category&id_category=$1$3 last;
        rewrite "^/([a-zA-Z0-9-]*)/([0-9]+)\-([a-zA-Z0-9-]*)\.html(.*)$" /index.php?controller=product&id_product=$2$4 last;
        rewrite "^/([0-9]+)__([a-zA-Z0-9-]*)(.*)$" /index.php?controller=supplier&id_supplier=$1$3 last;
        rewrite "^/([0-9]+)_([a-zA-Z0-9-]*)(.*)$" /index.php?controller=manufacturer&id_manufacturer=$1$3 last;
        rewrite "^/content/([0-9]+)\-([a-zA-Z0-9-]*)(.*)$" /index.php?controller=cms&id_cms=$1$3 last;
        rewrite "^/content/category/([0-9]+)\-([a-zA-Z0-9-]*)(.*)$" /index.php?controller=cms&id_cms_category=$1$3 last;
        rewrite "^/module/([_a-zA-Z0-9-]*)/([_a-zA-Z0-9-]*)$" /index.php?fc=module&module=$1&controller=$2 last;


        rewrite ^/page-non-trouvee$ /index.php?controller=404 last;
        rewrite ^/adresse$ /index.php?controller=address last;
        rewrite ^/adresses$ /index.php?controller=addresses last;
        rewrite ^/authentification$ /index.php?controller=authentication last;
        rewrite ^/meilleures-ventes$ /index.php?controller=best-sales last;   
        rewrite ^/panier$ /index.php?controller=cart last;
        rewrite ^/contactez-nous$ /index.php?controller=contact-form last;
        rewrite ^/bons-de-reduction$ /index.php?controller=discount last; 
        rewrite ^/suivi-commande-invite$ /index.php?controller=guest-tracking last;
        rewrite ^/historique-des-commandes$ /index.php?controller=history last;
        rewrite ^/identite$ /index.php?controller=identity last;
        rewrite ^/manufacturers$ /index.php?controller=manufacturer last;
        rewrite ^/mon-compte$ /index.php?controller=my-account last;
        rewrite ^/nouveaux-produits$ /index.php?controller=new-products last;
        rewrite ^/commande$ /index.php?controller=order last;
        rewrite ^/details-de-la-commande$ /index.php?controller=order-follow last;
        rewrite ^/commande-rapide$ /index.php?controller=order-opc last;
        rewrite ^/avoirs$ /index.php?controller=order-slip last;
        rewrite ^/mot-de-passe-oublie$ /index.php?controller=password last;
        rewrite ^/promotions$ /index.php?controller=prices-drop last;
        rewrite ^/recherche$ /index.php?controller=search last;
        rewrite ^/plan-du-site$ /index.php?controller=sitemap last;
        rewrite ^/magasins$ /index.php?controller=stores last;
        rewrite ^/supplier$ /index.php?controller=supplier last;

        rewrite ^/page-not-found$ /index.php?controller=404 last;
        rewrite ^/address$ /index.php?controller=address last;   
        rewrite ^/addresses$ /index.php?controller=addresses last;
        rewrite ^/authentication$ /index.php?controller=authentication last;
        rewrite ^/best-sales$ /index.php?controller=best-sales last;
        rewrite ^/cart$ /index.php?controller=cart last;
        rewrite ^/contacts$ /index.php?controller=contact-form last;
        rewrite ^/discount$ /index.php?controller=discount last;
        rewrite ^/guest-tracking$ /index.php?controller=guest-tracking last;
        rewrite ^/order-history$ /index.php?controller=history last;
        rewrite ^/identity$ /index.php?controller=identity last;
        rewrite ^/manufacturers$ /index.php?controller=manufacturer last;
        rewrite ^/my-account$ /index.php?controller=my-account last;
        rewrite ^/new-products$ /index.php?controller=new-products last;

        rewrite ^/order$ /index.php?controller=order last;
        rewrite ^/order/step([0-9]).html$ /index.php?controller=order&step=$1 last;
        rewrite ^/order-follow$ /index.php?controller=order-follow last;
        rewrite ^/quick-order$ /index.php?controller=order-opc last;
        rewrite ^/order-slip$ /index.php?controller=order-slip last;
        rewrite ^/password-recovery$ /index.php?controller=password last;
        rewrite ^/prices-drop$ /index.php?controller=prices-drop last;   
        rewrite ^/search$ /index.php?controller=search last;
        rewrite ^/sitemap$ /index.php?controller=sitemap last;
        rewrite ^/stores$ /index.php?controller=stores last;  
        rewrite ^/supplier$ /index.php?controller=supplier last;

    if (!-e $request_filename){
                rewrite ^(.*)$ /index.php?q=$uri&$args last;
        }


And the server block to use it:

Server definition file

nano /etc/nginx/sites-avaiable/www.myshop.com.conf

  
server {  
    listen        80;
    server_name     www.myshop.com;
    server_name_in_redirect off;
    root         /var/websites/www.myshop.com/public;
    index       index.php index.htm index.html;

    access_log      /var/log/nginx/access.myshop.co.uk.log;
    error_log   /var/log/nginx/error.myshop.co.uk.log debug;

    include     shopapps.d/prestashop-v14.conf;
    include     shopapps.d/drop.conf;
    include     shopapps.d/php.conf;
}



This config is then made active by symlinking into into the sites-enabled folder:

cd /etc/nginx/sites-enabled ln -s ../sites-avaiable/www.myshop.com.conf


Other config Files

You will notice there are a number of other shared config files included under this website server definition. For reference below are their contents:

nano /etc/nginx/shopapps.d/drop.conf

This file is a must have to secure certain files on your website:


    location = /robots.txt  { allow all; access_log off; log_not_found off; }
    location = /favicon.ico { access_log off; log_not_found off; }      
    # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
    # Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
    location ~ /\.          { deny all; }
    location ~ ~$           { access_log off; log_not_found off; deny all; }
    location = /CVS         { deny all;}

    location ~ (\.inc\.php|\.tpl|\.sql|\.tpl\.php|\.db)$ { deny all; }
    location ~ /\.ht { deny all; }

    # Deny access to any files with a .php extension in the uploads directory
    # Works in sub-directory installs and also in multisite network
    # Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
    location ~* /(?:uploads|files)/.*\.php$ {
        deny all;
    }
    location ~ ^/(README|INSTALL|LICENSE|CHANGELOG|UPGRADING)$ {
        deny all;
    }
    location ~ ^/(bin|SQL)/ {
        deny all;
    }


nano /etc/nginx/shopapps.d/php.conf

On my server I use nginx and php-fpm running as a socket to manage php requests as i find this the most efficient method in respect to resources on the server.

  
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index    index.php;
        fastcgi_read_timeout 7200;  # 2 hours for big cron jobs...
        include fastcgi_params;
    }


Barring any typo's on my part, you should now have a working config supporting Prestashop 1.6 & muti-site on nginx.

Now you just need to restart the server...

Step 3

Restart the server

On Ubuntu the commands are:

nginx -t && service nginx reload

This is actually two commands on one line. The first part nginx -t tests the configuration, if the console reports any errors then deal with them appropriately. Then service nginx reload actually reloads the nginx server.


Want to know more - Then why not subscribe to our mailing list?

* indicates required
Which information are you interested in?