Redirect 设置Nginx重定向规则优先级

Redirect 设置Nginx重定向规则优先级,redirect,nginx,rewrite,Redirect,Nginx,Rewrite,我有nginx+phpfpm和一些sef URL的重写规则 问题是我的所有自定义重定向/重写都被忽略,请求转到php脚本,而不是重定向 sef链接部分: if ($request_filename !~ ".(png|gif|ico|swf|jpe?g|js|css)$"){ set $rule_0 1$rule_0; } if (!-f $request_filename){ set $rule_0 2$rule_0; } if (!-d $requ

我有nginx+phpfpm和一些sef URL的重写规则

问题是我的所有自定义重定向/重写都被忽略,请求转到php脚本,而不是重定向

sef链接部分:

if ($request_filename !~ ".(png|gif|ico|swf|jpe?g|js|css)$"){
        set $rule_0 1$rule_0;
}   
if (!-f $request_filename){
        set $rule_0 2$rule_0;
}   
if (!-d $request_filename){
        set $rule_0 3$rule_0;
}   
if ($rule_0 = "321"){
        rewrite /. /index.php?sef_rewrite=1 last;
}   
我想这样做:

location = /first.html {
    return 301 /second.html;
    }

您需要了解nginx的位置和规则。这样,编写正确的配置就容易多了。试试这个:

location / {
    # replacement for last three `if`s
    try_files $uri $uri/ /index.php?sef_rewrite=1;
}

# here is you redirect
location = /first.html {
    return 301 /second.html;
}

# this is replacement for first `if`
location ~ \.(png|gif|ico|swf|jpe?g|js|css)$ {
    # serve static files
}

# I'm sure you have this block somewhere
location ~ \.php$ {
    # serve php
}
这些文章也值得一读:


谢谢。现在我看到了“真实”的方式。但我有一个新的“惊喜”,我所有的ajax请求都失败了。我想它们只是被/index.php?sef_rewrite=1重写的,我猜不出您的ajax请求是什么,以及它们是如何失败的。显示完整的nginx配置以及失败的具体内容