nginx rewrite rule under a subdirectory

2019-02-16 21:56发布

问题:

I have a WordPress site running nginx under a sub-direcotry. how can i write rewrite rules in a sub-directory? or can anyone please convert this Apache rewrite rule? I searched everywhere about nginx rewrite rules but nothing worked!

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

any help appreciated, thanks

回答1:

try to use this, and please don't forget to replace root path!

location /main/ {
   root /full/path/from/root/main/;
   try_files $uri $uri/ /index.php?$args;
}

I've set wordpress on my host in folder /main and got it's working with next settings:

location /main {
        index index.php;
        try_files $uri $uri/ /main/index.php?q=$uri;
}
root /path/to/webroot;