Nignx在搜索查询上重写模式?

时间:2015-08-05 07:32:53

标签: .htaccess mod-rewrite nginx url-rewriting

这是我需要做的一件非常基本的事情,但我无法想象这一点。

我的nginx.conf中有以下规则

    location ^/search {
        #try_files index.php?search=$1  last;
        rewrite ^/search/(.*)/ index.php?search=$1;
        #rewrite ^/search/([^/]+)/page/([^/]+)/?$ /index.php?search=$1&page=$2;
    }

    location ^/article {
        rewrite ^/article/([^/]+)/([^/]+)/$ /index.php?layout=article&id=$1;
    }

    location ^/images {
        rewrite ^/images/([^/]+)$ /images/$1 break;
        rewrite ^/images/([^/]+)/([^/]+)$ /images/$1/$2 break;
    }

    location ^/css {
        rewrite ^/css/([^/]+)$ /css/$1 break;
        rewrite ^/css/([^/]+)/([^/]+)$ /css/$1/$2 break;
    }

    location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
        root $root_path;
    }

    location ~ /\.ht {
        deny all;
    }

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include /etc/nginx/fastcgi_params;
    }

基本上我想做的就是 匹配下面的搜索查询以上。

example.com/search-term1/
or
example.com/search term/
or 
example.com/search/search-term1/
or
example.com/search/search-term1/page/1/

以上网址模式?我该怎么做?上面的规则给我错误500内部服务器错误。

在Nginx日志中我得到了

2015/08/05 12:56:39 [error] 11136#0: *2 could not find named location "@rewrite", client: 202.172.107.194, server: example.com, request: "GET /search/search-term1/page/1/ HTTP/1.1", host: "example.com"

1 个答案:

答案 0 :(得分:0)

我通过将它们移出位置块并将它们直接放入服务器块来修复此问题。