开发者社区 问答 正文

求NGINX下的伪静态规则。。。。

求 NGINX 下的伪静态规则 。。。。
http://www.abc.com/7.mp3
跳转到
http://www.abc.com/content/themes/ams/playsong.php?song=7


网站已有 WP的规则了,如何增加上面的规则到这里 。。。
location / {
        if (-f $request_filename/index.html){
                rewrite (.*) $1/index.html break;
        }
       if (-f $request_filename/index.php){
               rewrite (.*) $1/index.php;
        }         if (!-f $request_filename){
                rewrite (.*) /index.php;
        } }


请帮忙。。。。。

展开
收起
520vc 2014-08-06 16:35:24 5531 分享 版权
阿里云 AI 助理回答

要在已有的WordPress伪静态规则中添加新的跳转规则,你可以在nginx的配置文件中的server块里,定位到你的location /配置段后面,然后插入一个新的location或者直接使用rewrite指令来实现这个特定的重写需求。这里是一个示例,展示了如何添加一个针对.mp3请求的重定向规则:

# 已有的 WordPress 伪静态规则
location / {
    if (-f $request_filename/index.html){
        rewrite (.*) $1/index.html break;
    }

    if (-f $request_filename/index.php){
        rewrite (.*) $1/index.php;
    }

    if (!-f $request_filename){
        rewrite (.*) /index.php;
    }
}

# 新增的针对 .mp3 请求的重定向规则
location ~* ^.+\.mp3$ {
    rewrite ^/([^/]+)\.mp3$ /content/themes/ams/playsong.php?song=$1 last;
    # 或者使用 return 指令
    # return 301 /content/themes/ams/playsong.php?song=$1;
}

这段代码中,location ~* ^.+\.mp3$会匹配所有以.mp3结尾的请求,然后通过rewrite指令将请求重定向到你指定的PHP脚本,并且通过正则表达式捕获URL中的数字部分作为song参数传递。

请注意,修改nginx配置后,需要重启nginx服务使更改生效。在Linux系统中,通常可以通过以下命令来完成重启:

sudo service nginx restart

sudo systemctl restart nginx

请根据你的系统和权限情况选择合适的命令执行。

有帮助
无帮助
AI 助理回答生成答案可能存在不准确,仅供参考
0 条回答
写回答
取消 提交回答