设置Nginx重定向规则优先级

时间:2014-08-05 20:56:44

标签: redirect nginx rewrite

我有nginx + php-fpm和sef-urls的一些重写规则。

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

sef-links的一部分:

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;
    }

1 个答案:

答案 0 :(得分:0)

你需要根据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
}

这些文章也值得一读: