Apache 到 Nginx 重写与子字符串匹配的非资产


Apache to Nginx rewrite for non assets matching a substring

我有以下Apache重写条件,只是无法弄清楚将其转换为Nginx重写的正确方法是什么。

RewriteCond %{REQUEST_URI} !('.[a-zA-Z0-9]{1,5})$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/(site|search|members|P[0-9]{2,8}) [NC]
RewriteRule ^(.*)$ /index.php?/$1 [L]

编辑:事实证明,页面上的一条规则劫持了我以前的规则。有问题的规则是这样的:

if (!-d $request_filename) {
    rewrite ^/(.+)/$ /$1 permanent;
}

您应该将两个REQUEST_URI条件组合到一个正则表达式中,并将其用作位置说明符。然后在位置内检查文件是否存在。

location ~ ^'/(site|search|members|P[0-9]{2,8})(?!('.[a-zA-Z0-9]{1,5}$)) {
    if (!-e $request_filename) {
        rewrite ^(.*)$ /index.php?/$1 break;
    }
    # here goes something like 'root ..' for getting existent file
}

这应该可以解决问题。

# nginx configuration
location ~ "('.[a-zA-Z0-9]{1,5})$" {
}
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?/$1 break;
}
}

希望这对:)有所帮助