【问题标题】:NGINX Rewrites dynamic pagesNGINX 重写动态页面
【发布时间】:2017-06-25 20:51:16
【问题描述】:

我需要一些关于 nginx 正则表达式重写的帮助。

这个

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/

这个(棘手的)。

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

到

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

感谢大家的帮助。

@米格尔·莫塔

标记

【问题讨论】:

    标签: regex nginx url-rewriting


    【解决方案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;
        }
    }
    

    【讨论】:

    • 谢谢 Miguel,我现在正在测试它。还有 - 如果#C /forums/pc-probs 等于其他东西,/forums/tesco.8 或 /forums/otherforum.47 或 /forums/another-forum.48 等等....这可以是动态的吗
    • @MarkDavis 原始网址中的pc-probs 在哪里?是查询参数吗?
    • #A 和 #B 正在工作,但在 xenforo 上使用 #B 时出现 javascript 错误。
    • pc-probs 是软件生成的论坛名称。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    相关资源
    最近更新 更多