为NGINX重写格式国防部重写规则

为NGINX重写格式国防部重写规则

本文介绍了该转换使用%(QUERY_STRING)为NGINX重写格式国防部重写规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经被困的最后几天尝试转换下的Apache / mod_rewrite的重写规则NGINX格式。任何人都知道我要去的地方错了?

mod_rewrite的:

 的RewriteCond%{QUERY_STRING}主题=([0-9] +)
重写规则/forum/index\.php /forum/vbseo301.php?action=thread&oldid=%1 [L]
 

NGINX:

 位置/forum/index.php {
           如果($的args〜主题=([0-9] +)){
                        改写^ /论坛/指数\ .PHP主题=(+)$ /forum/vbseo301.php?action=thread&oldid=$1最后?;
                        }
        }
 

解决方案

好像很多人还在看这个问题。希望我的回答可以帮助!

 位置/forum/index.php {
  如果($的args〜主题=([0-9] +)){
    改写^ /论坛/指数\的.php $ /forum/vbseo301.php?action=thread&oldid=$arg_topic?持续;
  }
}
 

经过测试,它的工作原理。在改写行一对夫妇的关键,但微妙的修复:

  1. 我去掉了查询字符串的一部分?主题=(。+)从旧的URL,因为nginx的重写规则根本不(在nginx的也称为参数)匹配查询字符串。它只是尝试匹配路径。 参考链接
  2. 参数话题= xxxx是自动存储在nginx的这个变量:$ arg_topic。所以,我可以简单地把OLDID = $ arg_topic的新网址。 参考链接
  3. 在我打上问号?在新的URL的末尾,这样的nginx不会尝试从旧的URL连接的查询字符串。默认情况下,nginx的将附加整个查询字符串从旧的URL。

I've been stuck the last few days trying to convert the following Apache/Mod_Rewrite rewrite rule to NGINX format. Anyone know where I am going wrong?

MOD_REWRITE:

RewriteCond %{QUERY_STRING} topic=([0-9]+)
RewriteRule /forum/index\.php /forum/vbseo301.php?action=thread&oldid=%1 [L]

NGINX:

location /forum/index.php {
           if ($args ~ "topic=([0-9]+)"){
                        rewrite ^/forum/index\.php?topic=(.+)$ /forum/vbseo301.php?action=thread&oldid=$1 last;
                        }
        }
解决方案

Seems lots of people are still viewing this question. Hope my answer helps!

location /forum/index.php {
  if ($args ~ "topic=([0-9]+)"){
    rewrite ^/forum/index\.php$ /forum/vbseo301.php?action=thread&oldid=$arg_topic? last;
  }
}

Tested and it works. A couple of critical but subtle fixes in the rewrite line:

  1. I removed the query string part "?topic=(.+)" from the old url because nginx rewrite rule doesn't match query strings at all(also called args in nginx). It only tries to match PATH. reference link
  2. The argument topic=xxxx is automatically stored in nginx in this variable: $arg_topic. So I can simply put "oldid=$arg_topic" in the new url. reference link
  3. I put a question mark "?" at the end of the new url so that nginx won't try to attach the query string from the old url. By default, nginx will attach the entire query string from the old url.

这篇关于该转换使用%(QUERY_STRING)为NGINX重写格式国防部重写规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 00:18