NGINX Rewrites dynamic pages

早过忘川 提交于 2019-12-23 04:26:22

问题


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


来源:https://stackoverflow.com/questions/44750493/nginx-rewrites-dynamic-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!