nginx rewrite rule under a subdirectory

13,858

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;
Share:
13,858
Admin
Author by

Admin

Updated on June 23, 2022

Comments

  • Admin
    Admin almost 2 years

    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

  • Admin
    Admin over 12 years
    thanks Sergei. but it didn't work and redirects to my domain root without pictures and css (domain.com) but WordPress site is in domain.com/main/
  • Sergei Lomakov
    Sergei Lomakov over 12 years
    Could you please attach nginx.conf?
  • Admin
    Admin over 12 years
    Thanks, I've added the code to default.conf and restarted nginx but when i open my website, no image and css are loaded, it's just text and links redirect to the same page as domain.com/main. custom: /%postname%/
  • Sergei Lomakov
    Sergei Lomakov over 12 years
    You have to add root path for your virtual host inside server directive block.
  • Ryan
    Ryan almost 10 years
    @SergeiLomakov you'd be my here too if you have ideas about stackoverflow.com/questions/23645386/…! I still can't figure it out! Thanks!