【发布时间】:2017-08-28 11:30:05
【问题描述】:
当我使用此请求发送参数 example.com/index.php?text=s1&sort=s2 时,如何在 nginx 中重写规则,但我希望 nginx 将其处理为example.com/process/?text=s1&sort=s2 ?
“s1”和“s2”是您在搜索表单中输入的内容。
我已经试过了:
rewrite ^/index.php?text=(.*)$ /process/?text=$1 last;
还有这个:
location ~* /index.php?text=(.*)$ {
try_files $uri /search/?text=$1;
#try_files $uri /search/?text=$is_args$args;
}
还有这个..
location =index.php?text=$1&sort=$2 {
rewrite index.php?text=$1&sort=$2 /process/text=$1&sort=$2;
}
但不知何故,它不起作用。
这是我配置的主要部分:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~* \.php$ {
include /etc/nginx/php_params;
include /etc/nginx/fastcgi_params;
}
我很困惑..:/
【问题讨论】:
-
请不要在这里使用
.htaccess标签——它不适用于nginx。我已经删除了标签。 -
/index.php是否用于其他用途?或者我可以重写任何以/index.php开头的URI吗? -
是的,它已经用于缩短其他 url 路径。例如,我可以使用“site.com/register”来这里 -> site.com/index,php/?register 或 site.com/login -> site.com/index.php?/login。实际上,它适用于 Codeigniter 框架。
-
抱歉,我在原始请求中拼错了一个单词。不是 example.com/index.php?q=s1&sort=s2 ,而是 example.com/index.php?text=s1&sort=s2 - 现在已修复。检查你的答案..
标签: nginx url-rewriting