【问题标题】:Nginx Rewrite Multiple Query ParametersNginx 重写多个查询参数
【发布时间】:2017-01-18 18:39:48
【问题描述】:

我在创建与我在 Nginx 上的 .htaccess 匹配的重写规则时遇到问题,我希望有人能指出我正确的方向。

我的 .htaccess

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt|app|assets|upload|api)
RewriteRule ^([^/]+)(?:/([^/]+)|)(?:/([^/]+)|)(?:/([^/]+)|) index.php?class=$1&action=$2&param=$3 [L]

我的 nginx 配置

location / {
    rewrite ^/([^/]+)(?:/([^/]+)|)(?:/([^/]+)|)(?:/([^/]+)|) /index.php?class=$1&action=$2&param=$3 break;
}

查询参数是可选的,例如:

site.com/
site.com/customers
site.com/customers/add
site.com/customers/edit/1

我无法重写应用程序代码,因为 iam 只是从 apache 迁移到 nginx。

我们将不胜感激。

谢谢。

【问题讨论】:

    标签: .htaccess mod-rewrite nginx url-rewriting query-parameters


    【解决方案1】:

    您可以像这样构建解决方案:

    root /path/to/root;
    index index.php;
    
    location / {
        try_files $uri $uri/ @rewrite;
    }
    location @rewrite {
        rewrite ^/([^/]+)(?:/([^/]+)|)(?:/([^/]+)|)(?:/([^/]+)|) /index.php?class=$1&action=$2&param=$3 last;
        return 404;
    }
    location ~ \.php$ {
        try_files $uri =404;
    
        # fastcgi stuff
    }
    

    我没有检查你的正则表达式。 try_files 指令是documented here。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-29
      • 2015-08-26
      • 1970-01-01
      • 2013-09-04
      • 2016-02-27
      • 2012-10-12
      • 1970-01-01
      • 2011-02-12
      相关资源
      最近更新 更多