title: Magento. Nginx. Multiple store. How to add 'index.php' to urls
body: <p>I downloaded magento with 3 stores and trying to install it on localhost.</p>
  
  <ul>
  <li>Ubuntu 14</li>
  <li>Nginx</li>
  <li>Mysql</li>
  <li><p>php-fpm</p>
  
  <pre><code>/app
  /downloader
  /includes
  ...
  /store1/index.php
  /store2/index.php
  </code></pre></li>
  </ul>
  
  <p>Inside my <code>store1/index.php</code> I put <code>$mageRunCode = 'my_store_code'</code></p>
  
  <p>When I open url <code>http://mysite.local.dev/store1/</code> everything is opening properly but all links don't contain 'index.php', without it all urls don't opening.</p>
  
  <p>Returns 404:</p>
  
  <blockquote>
    <p><a href="http://mysite.local.dev/store1/some-cms-page.html" rel="nofollow">http://mysite.local.dev/store1/some-cms-page.html</a></p>
  </blockquote>
  
  <p>Opening well:</p>
  
  <blockquote>
    <p><a href="http://mysite.local.dev/store1/index.php/some-cms-page.html" rel="nofollow">http://mysite.local.dev/store1/index.php/some-cms-page.html</a></p>
  </blockquote>
  
  <p>Could you please tell me how to make url rewrite to add 'index.php' to my urls, or please advice another more clear solution.</p>
  
  <p>Thanks in advance</p>
  
  <pre><code>server {
  
      listen 80;
      server_name local.dev *.local.dev;
      root /var/www/local.dev/www/$subdomain;
      set $subdomain "";
      if ($host ~* ^([a-z0-9-\.]+)\.local.dev$) {
          set $subdomain $1;
      }
      if ($host ~* ^www.local.dev$) {
          set $subdomain "";
      }
  
      location / {
          index index.html index.php; ## Allow a static html file to be shown first
          try_files $uri $uri/ @handler; ## If missing pass the URI to Magento's front handler
          expires 30d; ## Assume all files are cachable
      }
  
      ## These locations would be hidden by .htaccess normally
      location ^~ /app/                { deny all; }
      location ^~ /includes/           { deny all; }
      location ^~ /lib/                { deny all; }
      location ^~ /media/downloadable/ { deny all; }
      location ^~ /pkginfo/            { deny all; }
      location ^~ /report/config.xml   { deny all; }
      location ^~ /var/                { deny all; }
  
      location /var/export/ { ## Allow admins only to view export folder
          auth_basic           "Restricted"; ## Message shown in login window
          auth_basic_user_file htpasswd; ## See /etc/nginx/htpassword
          autoindex            on;
      }
  
      location  /. { ## Disable .htaccess and other hidden files
          return 404;
      }
  
      location /api {
          rewrite ^/api/rest /api.php?type=rest last;
      }
  
      location @handler { ## Magento uses a common front handler
          rewrite / /index.php;
      }
  
      location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
          rewrite ^(.*.php)/ $1 last;
      }
  
      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 unix:/var/run/php5-fpm.sock;
          fastcgi_read_timeout 600; 
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
          fastcgi_param  MAGE_RUN_CODE default; ## Store code is defined in administration &gt; Configuration &gt; Manage Stores
          fastcgi_param  MAGE_RUN_TYPE store;
          include        fastcgi_params; ## See /etc/nginx/fastcgi_params
      }
  
  }
  </code></pre>
  
  <hr />
  
  <h1>According to Emi`s solution.</h1>
  
  <p>If I clearly understand you - here is updated config:</p>
  
  <pre><code>server {
  
      listen 80;
      server_name local.dev *.local.dev;
      root /var/www/local.dev/www/$subdomain;
      set $subdomain "";
      set $magento_run_code "";
      if ($host ~* ^([a-z0-9-\.]+)\.local.dev$) {
          set $subdomain $1;
          set $magento_run_code $1;
      }
      if ($host ~* ^www.local.dev$) {
          set $subdomain "";
          set $magento_run_code "";
      }
  
      location / {
          index index.html index.php; ## Allow a static html file to be shown first
          try_files $uri $uri/ @handler; ## If missing pass the URI to Magento's front handler
          expires 30d; ## Assume all files are cachable
      }
  
      ## These locations would be hidden by .htaccess normally
      location ^~ /app/                { deny all; }
      location ^~ /includes/           { deny all; }
      location ^~ /lib/                { deny all; }
      location ^~ /media/downloadable/ { deny all; }
      location ^~ /pkginfo/            { deny all; }
      location ^~ /report/config.xml   { deny all; }
      location ^~ /var/                { deny all; }
  
      location /var/export/ { ## Allow admins only to view export folder
          auth_basic           "Restricted"; ## Message shown in login window
          auth_basic_user_file htpasswd; ## See /etc/nginx/htpassword
          autoindex            on;
      }
  
      location  /. { ## Disable .htaccess and other hidden files
          return 404;
      }
  
      location /api {
          rewrite ^/api/rest /api.php?type=rest last;
      }
  
      location @handler { ## Magento uses a common front handler
          rewrite / /index.php;
      }
  
      location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
          rewrite ^(.*.php)/ $1 last;
      }
  
      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 unix:/var/run/php5-fpm.sock;
          fastcgi_read_timeout 600; 
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
          fastcgi_param  MAGE_RUN_CODE $magento_run_code; ## Store code is defined in administration &gt; Configuration &gt; Manage Stores
          fastcgi_param  MAGE_RUN_TYPE store;
          include        fastcgi_params; ## See /etc/nginx/fastcgi_params
      }
  
  }
  </code></pre>
  
  <p>In my core_store table i have following (names are replaced):</p>
  
  <pre><code>default - sitename
  default_store1 - store1
  default_store2 - store2
  </code></pre>
  
  <p>unsecure urls are:</p>
  
  <pre><code>http://sitename.local.dev/
  http://sitename.local.dev/store1/
  http://sitename.local.dev/store2/
  </code></pre>
  
  <p>when I'm trying to open <strong>sitename.local.dev</strong> its trying to open with <strong>$mage_run_code = sitename</strong>. That because I'm getting 404. I think the expected value should be <strong>default</strong>.</p>
  
  <p>What should be the url for store1? sitename.local.dev/store1 ?</p>
  
  <p><strong>upd.1</strong>
  I understood your idea.</p>
  
  <p>When we had </p>
  
  <pre><code>    if ($host ~* ^([a-z0-9-\.]+)\.local.dev$) {
          set $subdomain $1;
          set $magento_run_code $1;
      }
  </code></pre>
  
  <p>magento started executing with $1 which equals value in 3rd domain lvl. In my case <a href="http://sitename.local.dev" rel="nofollow">http://sitename.local.dev</a> - it is <strong>sitename</strong>.</p>
  
  <p>I changed it to </p>
  
  <pre><code>    if ($host ~* ^([a-z0-9-\.]+)\.local.dev$) {
          set $subdomain $1;
          set $magento_run_code default;
      }
  </code></pre>
  
  <p>now it works well for first store.</p>
  
  <p>Lets apply your solution for store1</p>
  
  <h1>According to this solution my current question is:</h1>
  
  <blockquote>
    <p>directory: {magento_root}/store1/*</p>
    
    <p>mage_run_code: default_store1</p>
    
    <p>url: <a href="http://store1.local.dev" rel="nofollow">http://store1.local.dev</a> or <a href="http://sitename.local.dev/store1/" rel="nofollow">http://sitename.local.dev/store1/</a> ??????</p>
    
    <p>nginx config: as far as I can see it will depends on url. which url and which config should I use?</p>
  </blockquote>