nginx掩码URL不重定向

时间:2016-06-26 00:10:11

标签: php regex nginx url-masking

我有一个在nginx上运行的网站。

在我的nginx / sites-available / default文件中我有一行:

SELECT [name], 
       SUM([rating] <= 15) / Count(*) AS bad_rating
FROM Reports
GROUP BY [name]

这样做是使examplesite.com转发到examplesite.com/search.php?path=$1,然后屏蔽该新URL。因此,网址看起来像“examplesite.com”,其操作类似于examplesite.com/search.php?path=$1

我想做点不同的事情。

我想完全不改变examplesite.com的功能,而是将url examplesite.com/search.php?path=$1显示为examplesite.com/$1并保持其相同的功能。

所以我不想重定向,只需将examplesite.com/search.php?path=$1显示为examplesite.com/$1

但是,似乎重写无法处理此功能。是否可以在nginx中使用该功能?

编辑1

例如:

我有网址http://www.examplesite.com/search.php?search=house&location=chicago

我想保留这些功能,但只需将其显示为:

http://www.examplesite.com/chicago/house

编辑2

我的代码发生了变化:

http://www.examplesite.com/search.php?search_title=apple&search_location=boston

为:

http://www.examplesite.com/boston/apple?search_title=apple&search_location=boston

这是代码:

rewrite ^(.*)$ /search.php?path=$1 break;

但问题是这会导致网站上的PHP代码失败,并且最后仍会显示所有参数。

编辑3

按照评论中的建议查看:https://serverfault.com/questions/160790/nginx-rewrite-for-an-url-with-parameters后,我现在可以显示正确的网址:

http://www.examplesite.com/boston/apple

添加?最后制作我当前的代码:

if ($args ~* "search_title=[a-z]*&search_location=[a-z]*") {
    rewrite ^/search.php?(.*)$ http://www.examplesite.com/$arg_search_location/$arg_search_title last;
}

但是这个网址是PHP正在阅读的网址。所以,我得到一个404 Not Found,这是有道理的。

是否可以让PHP读取旧网址但是现在在浏览器中显示网址?

编辑4

我不确定,但我认为nginx的重写功能可能无法做我想做的事情,所以我看着proxy_pass和proxy_set_header

编辑5

我有第二个问题,从单独的角度解决问题:Nginx redirect URL into a PHP GET url

0 个答案:

没有答案