如何将 .htaccess 转换为等效的 nginx?

How to convert .htaccess to a nginx equivalent?

如何将.htaccess配置转换为nginx?

我的.htacess:

Options -MultiViews
RewriteEngine On
RewriteBase /mvc/public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url= [QSA,L]

我试过这个:

nginx 配置

location /mvc/public/ {
if (!-e $request_filename){
rewrite ^/mvc/public/(.+)$ /mvc/public/index.php?url= [QSA,L];
}
}

但这没有用! 有人可以帮助我吗?

[QSA,L] 不是 nginx 语法 - 有关详细信息,请参阅 this document

location /mvc/public/ {
    if (!-e $request_filename) {
        rewrite ^/mvc/public/(.+)$ /mvc/public/index.php?url= last;
    }
}

可以使用 try_files 而不是 if 来完成类似的行为:

location /mvc/public/ {
    try_files $uri $uri/ @rewrite;
}
location @rewrite {
    rewrite ^/mvc/public/(.+)$ /mvc/public/index.php?url= last;
}

详情见this document