Nginx – the nginx equivalent to this mod_rewrite rule

mod-rewritenginxWordpress

My wordpress is telling me to update my htaccess file to:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

I'm using nginx, and I have this but it doesn't work:

 location / {
            root   /home/mywebsite/public;
            index  index.php index.html index.htm;

            if (!-e $request_filename) {
                        rewrite ~(.+)$ /index.php?q=$1 last;
            }
        }

Best Answer

try changing it to this;

   location / {
       root   /home/mywebsite/public;
       index index.php index.html index.htm;
       try_files $uri $uri/ /index.php;

   }

http://wiki.nginx.org/HttpCoreModule#try_files