NGINX Rewrites dynamic pages

2019-06-13 01:38发布

问题:

I need some help with nginx regex rewrites.

THIS

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

TO

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

THIS

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

TO

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

THIS (TRICKY ONE).

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

TO

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

Thank you all for your help.

@Miguel Mota

Mark

回答1:

Try this. This works based on your examples.

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;
    }
}