8

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

2 Answers2

17

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;
Sergei Lomakov
  • 2,021
  • 19
  • 18
  • 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/ –  Jan 16 '12 at 12:36
  • Could you please attach nginx.conf? – Sergei Lomakov Jan 16 '12 at 18:19
  • 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%/ –  Jan 18 '12 at 08:36
  • You have to add root path for your virtual host inside server directive block. – Sergei Lomakov Jan 18 '12 at 16:01
  • @SergeiLomakov you'd be my here too if you have ideas about http://stackoverflow.com/questions/23645386/how-to-install-wordpress-alongside-laravel-on-nginx-with-pretty-permalinks-seo! I still can't figure it out! Thanks! – Ryan May 15 '14 at 01:18
0
location ^~ /main {
    root /var/www/domain.com/html/wordpress;
    index index.php;
    try_files $uri $uri/ /main/index.php;

    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass   unix:/run/php/php8.0-fpm.sock;
    }
}

You need to change the php8.0 to your current PHP version in use and the root /var/www/domain.com/html/wordpress to your root directory where the subdirectory main comes after subdirectory Wordpress.

Abe
  • 391
  • 2
  • 20