简体   繁体   中英

nginx configuration targeting a folder like htaccess

I'm moving from Apache to nginx, have many .htaccess files throughout the site (eg example.com/.htaccess, example.com/folder/.htaccess).

An example of a .htaccess file I have is:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^do_ajax\.php /wp-admin/admin-ajax.php [QSA,L]
</IfModule>
# END WordPress

And the nginx conf equivalent is something like:

# BEGIN WordPress
rewrite ^/ajax /wp-admin/admin-ajax.php last;
# END WordPress

Since the nginx configuration goes in /etc/nginx/nginx.conf and targets all files instead of the folder, is there anything I can do to make it target the particular folder it's meant for (eg if the above folder for www.example.com is located at /usr/share/nginx/html/example/)?

You need to include the "folder" part in the match:

rewrite ^/folder/ajax /wp-admin/admin-ajax.php last;

since these nginx rules are not per directory context. But if you need to group them together you could use the location block :

location /folder/ {
   rewrite ^/folder/ajax /wp-admin/admin-ajax.php last;
}

if you want to group them together, but you still need the /folder/ in front of the regex.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM