【问题标题】:Nignx rewrite pattern on search query?Nginx 在搜索查询中重写模式?
【发布时间】:2015-10-27 20:27:05
【问题描述】:

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

我的 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/

上面的 url 模式?我该怎么做?上面的规则给了我错误 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"

【问题讨论】:

  • 显示完整配置。您也可以在location 指令中添加~ 标志或删除^。

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多