Nginx - URL重写规则

时间:2013-10-05 01:07:01

标签: mod-rewrite url-rewriting nginx

我有这个网址:

  

www.example.com/folder1/folder2/edit.php?username=nickname

其实我有这个重写规则:

location / {
    root   /var/www;
    index  index.php index.html index.htm;

    # First rewrite rule output: www.example.com/nickname

    rewrite ^/([A-Za-z0-9_]+)$ /folder1/folder2/user.php?username=$1;

    # Second rewrite rule output: www.example.com/nickname/edit

    rewrite ^/([A-Za-z]+)/edit$ /folder1/folder2/edit.php?username=$1;
}

然而,这很好。

但我需要像这样重写我的网址:

  

www.example.com/nickname/edit/info

它有几个参数:

  

www.example.com/folder1/folder2/edit.php?username=nickname&info=basic

我尝试了一切,但没有成功:

location / {
    root   /var/www;
    index  index.php index.html index.htm;

    # First rewrite rule output: www.example.com/nickname
    rewrite ^/([A-Za-z0-9_]+)$ /folder1/folder2/user.php?username=$1;

    # Second rewrite rule output: www.example.com/nickname/edit
    rewrite ^/([A-Za-z]+)/edit$ /folder1/folder2/edit.php?username=$1;

    # Here where I'm stuck
    # www.example.com/nickname/edit/info

    rewrite ^/edit/([A-Za-z]+)/info$ /folder1/folder2/edit.php?user=$1&info=$2;
}

1 个答案:

答案 0 :(得分:1)

您的上一次尝试似乎混淆了edit的位置。此外,没有任何内容传递给info,因为只有一个捕获组,也许

rewrite ^/([A-Za-z]+)/edit/([A-Za-z]+)$ /folder1/folder2/edit.php?username=$1&info=$2;

会改为吗?