2

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

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

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

这样做是使 examplesite.com 转发到 examplesite.com/search.php?path=$1,然后屏蔽该新 URL。因此,该 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

但是,似乎 rewrite 无法处理此功能。是否可以在 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

这是代码:

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 代码失败,最后仍然显示所有参数。

编辑 3

看了这个:https://serverfault.com/questions/160790/nginx-rewrite-for-an-url-with-parameters如评论中所建议的那样,我现在可以显示正确的 url:

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

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

是否可以让 PHP 读取旧 URL,但在浏览器中保留现在的 URL?

编辑 4

我不确定,但我在想nginx的rewrite函数可能无法做我想做的事,所以我在看proxy_pass和proxy_set_header

编辑 5

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

4

0 回答 0