2016-09-10 47 views
0

我在创建一个匹配Nginx上的.htaccess的重写规则时遇到了问题,我希望有人能指出我正确的方向。Nginx重写多个查询参数

我的.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。

任何帮助将不胜感激。

谢谢。

回答

0

你可以构建这样的解决方案:

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