user1049565
user1049565

Reputation:

nginx rewrite rule under a subdirectory

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

Upvotes: 8

Views: 15247

Answers (2)

Abe
Abe

Reputation: 431

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.

Upvotes: 1

Sergei Lomakov
Sergei Lomakov

Reputation: 2061

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;

Upvotes: 18

Related Questions