Prestashop 1.6.x nginx rewrite rules

I host my prestashop installations in nginx and I always use seo friendly urls in prestashop. Now, to use friendly url in nginx server, you will need to manually add the rewrite rule to your server location block.

If you don’t know anything about server blocks in nginx, please, learn about that first. So I just converted the .htaccess apache rewrite rules to nginx rules today. Here is my current location block with rewrite rules for nginx.

location / {

set $baseUri "";
rewrite ^/api$ /api/ break;
rewrite ^/api/(.*)$ /$baseUri/webservice/dispatcher.php?url=$1&$args break;
# Images
rewrite ^/([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /$baseUri/img/p/$1/$1$2$3.jpg break;
rewrite ^/([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /$baseUri/img/p/$1/$2/$1$2$3$4.jpg break;
rewrite ^/([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /$baseUri/img/p/$1/$2/$3/$1$2$3$4$5.jpg break;
rewrite ^/([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /$baseUri/img/p/$1/$2/$3/$4/$1$2$3$4$5$6.jpg break;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /$baseUri/img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6$7.jpg break;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /$baseUri/img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7$8.jpg break;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /$baseUri/img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8$9.jpg break;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /$baseUri/img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9$10.jpg break;
rewrite ^/c/([0-9]+)(\-[\.*_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ /$baseUri/img/c/$1$2$3.jpg break;
rewrite ^/c/([a-zA-Z_-]+)(-[0-9]+)?/.+\.jpg$ /$baseUri/img/c/$1$2.jpg break;
# AlphaImageLoader for IE and fancybox
rewrite ^images_ie/?([^/]+)\.(jpe?g|png|gif)$ js/jquery/plugins/fancybox/images/$1.$2 break;

#CSS


rewrite ^/css/(.*)\.css$ /$baseUri/css/retro-compat.css.php?file=$1.css&$args break;

try_files $uri $uri/ $baseUri/index.php?$args;

}

Now if your prestashop is hosted in a subdirectory under your domain, you should set $baseUri to that. like if your shop url is:

http://www.mysite.com/shop/

$baseUri needs to be set as “shop” (No need to put slashes).

Now I am not happy with the simple solution. I wanted to add expires headers along with rewrite rules in nginx. So, I had to break up the code into several location blocks like this:




	location / {


        set $baseUri "";


		rewrite ^/api$ /api/ break;
		rewrite ^/api/(.*)$ /$baseUri/webservice/dispatcher.php?url=$1&$args break;


		#CSS
		rewrite ^/css/(.*)\.css$ /$baseUri/css/retro-compat.css.php?file=$1.css&$args break;

 		try_files $uri $uri/ $baseUri/index.php?$args;

	}


	location ~* \.(?:gif|jpe?g|png)$
	{
		expires 30d;
		add_header Pragma public;
		add_header Cache-Control public;

		# Images
		rewrite ^/([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /$baseUri/img/p/$1/$1$2$3.jpg break;
		rewrite ^/([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /$baseUri/img/p/$1/$2/$1$2$3$4.jpg break;
		rewrite ^/([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /$baseUri/img/p/$1/$2/$3/$1$2$3$4$5.jpg break;
		rewrite ^/([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /$baseUri/img/p/$1/$2/$3/$4/$1$2$3$4$5$6.jpg break;
		rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /$baseUri/img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6$7.jpg break;
		rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /$baseUri/img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7$8.jpg break;
		rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /$baseUri/img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8$9.jpg break;
		rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /$baseUri/img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9$10.jpg break;
		rewrite ^/c/([0-9]+)(\-[\.*_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ /$baseUri/img/c/$1$2$3.jpg break;
		rewrite ^/c/([a-zA-Z_-]+)(-[0-9]+)?/.+\.jpg$ /$baseUri/img/c/$1$2.jpg break;
		# AlphaImageLoader for IE and fancybox
		rewrite ^images_ie/?([^/]+)\.(jpe?g|png|gif)$ js/jquery/plugins/fancybox/images/$1.$2 break;
	}

	location ~* \.(?:ico|css|js)$
	{
		expires 30d;
		add_header Pragma public;
		add_header Cache-Control public;
	}

  • First block handles everything except for images, css, and js files.
  • Second block handles image rewrites and cache headers
  • Third block handles icons, js, and css files, and cache headers

This is for 1.6.1.1 shop. If you happen to face any problem, do let me know in a comment.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s