Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex nginx重写多个参数_Regex_Nginx_Url Rewriting - Fatal编程技术网

Regex nginx重写多个参数

Regex nginx重写多个参数,regex,nginx,url-rewriting,Regex,Nginx,Url Rewriting,我正试图重写 web.com/abtoweb.com?sys=ab web.com/ab/cdtoweb.com?sys=ab&id=cd web.com保持不变 我写了以下内容: rewrite ^/(\w+)(/?\w*)$ /?system=$1&id=$2 break; 测试您的重写语句,我得到: web.com/abtoweb.com?sys=ab&id= web.com/ab/cd到web.com?sys=ab&id=/cd web.com保持不变 您可以通过移动括号来修复

我正试图重写

  • web.com/ab
    to
    web.com?sys=ab
  • web.com/ab/cd
    to
    web.com?sys=ab&id=cd
  • web.com
    保持不变
  • 我写了以下内容:

    rewrite ^/(\w+)(/?\w*)$ /?system=$1&id=$2 break;
    

    测试您的重写语句,我得到:

  • web.com/ab
    to
    web.com?sys=ab&id=
  • web.com/ab/cd
    web.com?sys=ab&id=/cd
  • web.com
    保持不变
  • 您可以通过移动括号来修复(2),以便不捕获第二个
    /
    。修复(1)的最简单方法是将一个
    rewrite
    语句替换为两个:

    rewrite ^/(\w+)/(\w*)$ /?system=$1&id=$2 break;
    rewrite ^/(\w+)$       /?system=$1       break;
    

    它适用于1个参数,但不适用于2。它适用于我。可能是您的配置中有其他原因导致您出现问题。更新了问题,添加了实际数据。您如何测试这些语句?我将配置粘贴到运行
    nginx/1.10.1
    的测试服务器中,我无法使其失败。是否在测试之间清除浏览器的缓存?陈旧的缓存可能导致不可预知的结果。@RichardSmith是的,当然。我测试了写
    ?system=test&id=5
    它工作了,但是
    /test/5
    没有t@RichardSmith当我访问
    /test/5
    时,它会尝试在
    /test/
    中查找我的js/css文件<代码>找不到web.com/test/user.css。重写是不正确的。那是另一个问题。如果对资源文件使用路径相对URI,并使用“看起来像”子目录的路由,则浏览器将通过在明显的子目录前加前缀来请求资源文件。您需要将路径绝对资源文件与此类方案一起使用。@RichardSmith但是如果URL被重写,浏览器应该看到
    ?system=2&id=5
    ,而不是子目录。。。