大网数据广告活动
服务器运维问答

Nginx - Redirect based on query string parameters

· 52CE 我爱测

We have a bit of a complicated problem to solve in our nginx configuration. Currently we have a piece of software installed in our document root. This software uses a single entry point (index.php) and query strings in order to s…

问题原文

原帖链接 · Kevin · CC BY-SA 4.0

We have a bit of a complicated problem to solve in our nginx configuration. Currently we have a piece of software installed in our document root. This software uses a single entry point (index.php) and query strings in order to show content. Example URLs are:

/index.php?forums/forum-name.1
/index.php?threads/thread-name.1
/index.php?users/user-name.1

Etc...

Now, we are moving this software into a subdirectory of /f/ and installing a new piece of software into the document root. This software ALSO uses index.php (No query strings, though). So we need to come up with a set of rewrite rules to ONLY rewrite the URLs from the old software. At the same time, we are also going to be removing the index.php from the URLs. A set of example mappings is:

/index.php?forums/forum-name.1 --> /f/forums/forum-name.1
/index.php?threads/thread-name.1 --> /f/threads/thread-name.1
/index.php?users/user-name.1 --> /f/users/user-name.1

So basically, I need to redirect a certain subset of index.php requests (Only containing ?forums, ?threads, ?users, etc...), then remove the index.php part and send to the /f/ directory.

I have played with this all morning and just can't get it to work how I need it to.

社区高分解答

原帖链接 · Richard Smith · CC BY-SA 4.0

To manage complex redirections, particularly when query strings are involved, the map directive can be used.

You cannot match the query string (anything from the ? onwards) in location and rewrite expressions, as it is not part of the normalized URI, however, the $request_uri contains the original request complete with query string.

Matching the $request_uri may be problematic if the parameters are not sent in a consistent order, also URIs containing strange characters will be percent encoded.

The map directive can match strings and/or regular expressions. See this document for details.

For example:

map $request_uri $redirect {
    default                                               0;
    ~*^/index\.php\?(?<suffix>(forums|threads|users).*)$  /f/$suffix;
    ...
}

server {
    ...
    if ($redirect) {
        return 301 $redirect;
    }
    ...
}

Always use named captures in a map block regular expression. The mapped expression is evaluated at the return statement. As I understand it, every time nginx encounters a statement containing a regular expression (such as rewrite, some location blocks, and some if statements), the numeric captures are reset. Using named captures ensures that they remain in scope at the return statement.

See this caution on the use of if.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-09-09)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。