NGINX重写动态页面

时间:2017-06-25 20:51:16

标签: regex nginx url-rewriting

我需要一些关于nginx regex重写的帮助。

https://www.website.com/downloads.php?do=file&id=4798

https://www.website.com/index.php?resources/4798/

https://www.website.com/showthread.php?t=4449128

https://www.website.com/index.php?threads/4449128/

这(TRICKY ONE)。

https://www.website.com/forumdisplay.php?f=12&prefixid=8

https://www.website.com/forums/pc-probs.12/?prefix_id=8

谢谢大家的帮助。

@Miguel Mota

标记

1 个答案:

答案 0 :(得分:1)

试试这个。这基于您的示例。

location / {

    # A
    if ($args ~* "id=(\d+)") {
        set $id $1;
        set $args '';

        rewrite ^/downloads\.php(.*)$ /index.php?resources/$id/ permanent;
    }

    # B
    if ($args ~* "t=(\d+)") {
        set $t $1;
        set $args '';

        rewrite ^/showthread.php(.*)$ /index.php?threads/$t/ permanent;
    }

    # C
    if ($args ~* "prefixid=(\d+)") {
        set $pfid  $1;
    }
    if ($args ~* "f=(\d+)") {
        set $f $1;
        set $args '';
        rewrite ^/forumdisplay.php(.*)$ /forums/pc-probs.$f/?prefix_id=$pfid/ permanent;
    }
}