Nginx – How to make Nginx include index.php in file paths for sub-directories as well (Magento Multistore)

magentonginxrewrite

Running multistore setup with Magento on an Nginx server. I'm watching the access logs and can plainly see what is happening. Like many PHP web apps, it uses index.php in its URLs and I have it set to "hide" it from the visible URL to keep it clean.

shoes.com/ is returning shoes.com/index.php
shoes.com/sneakers is returning shoes.com/sneakers/index.php

But if I try to go further I see that Nginx isn't including index.php for sub-directories as it is for the root directory. NOTE: Magento basically requires a modified index.php for its multistores that exist in sub-directories.

Checkout Example:

shoes.com/checkout/cart/ returns shoes.com/index.php/checkout/cart/

Whereas in a multistore directory:

shoes.com/sneakers/checkout/cart/ is returning shoes.com/sneakers/checkout/cart/ when it should be returning shoes.com/sneakers/index.php/checkout/cart/

I'm trying to figure out how to get it to apply index.php rules for this sub-directory as well. I have my Nginx configuration in 3 separate files; this is the rewrite file:

rewrite_log on;

    location / {
    index index.php index.html;
    try_files $uri $uri/ @handler;
    }

    location @handler {
    rewrite / /index.php;
    }

    ## force www in the URL
    if ($host !~* ^www\.) {
    #rewrite / $scheme://www.$host$request_uri permanent;
    }

    ## Forward paths like /js/index.php/x.js to relevant handler
    location ~ \.php/ {
    rewrite ^(.*\.php)/ $1 last;
    }

    location /media/catalog/ {
    expires 1y;
    log_not_found off;
    access_log off;
    }

    location /skin/ {
    expires 1y;
    }

    location /js/ {
    access_log off;
    }

    location ~ \.php$ { ## Execute PHP scripts
    if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss

    expires off; ## Do not cache dynamic content
    #fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.sock;    
fastcgi_index index.php;
    #fastcgi_param HTTPS $fastcgi_https;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    #fastcgi_param MAGE_RUN_CODE default; ## Store code is defined in administration > Configuration > Manage Stores
    #fastcgi_param MAGE_RUN_TYPE store;
    include fastcgi_params; ## See /etc/nginx/fastcgi_params

Best Answer

Part of this is Magento related, another nginx. First the Magento part: you need to make sure you have the base_link_url set correctly for the store view. You should only need index.php in the sneakers directory, with the correct MAGE_RUN_CODE.

As for nginx, you are telling it to pass all virtual requests to the root index.php, but all requests for /sneakers, should be routed to /sneakers/index.php:

location / {
    rewrite ^/ /index.php;
}

location /sneakers {
    rewrite ^/sneakers /sneakers/index.php;
}

But this isn't your only problem. I'll update the answer, when I'm not on my mobile.