繁体   English   中英

如何基于URI中特定参数的存在在Nginx中实现重写?

[英]How to implement rewrite in nginx based on existence of a specific argument in the URI?

最初的目标:我希望用户尝试打开http://example.com/something页面重定向到http://example.com/index.php?download=something

原始目标示例:访问源URLhttp://something.com/18AhPyB&p=CdATyJnFMw1gl9Z )的用户已重定向到目标URLhttp://something.com/index.php?download=18AhPyB&p=CdATyJnFMw1gl9Z

我在Nginx Config中所做的事情

location / {
rewrite ^/(.*)$ /index.php?download=$1 last; }

方法的问题:如果有人尝试打开主页http://something.com ),则显示错误。 我的理解是我无意间拨打了所有电话以进行重定向。

问题:我如何仅使用“&p”参数来使网址重定向? 请帮助。

^\/(.*&p=.*)$
  • ^是字符串开头。
  • \\ /是字符/转义。
  • ()捕获的组。
  • 。*匹配长度不受限制的任何字符。
  • &p =从字面上匹配&p =
  • $是字符串结尾。

Regex101( 在此处 )非常适合创建和调试正则表达式。

据我了解,仅当存在参数下载时,才必须重定向。 然后,您必须编写一个位置部分,如下所示:

index index.php;
location / {
    try_files $uri $uri/ /index.php;

    if ($arg_p) {
        return 301 http://something.com/index.php?download=$request_uri;
    }

    location = /index.php {

        fastcgi_pass   127.0.0.1:6969;
        fastcgi_param  SCRIPT_FILENAME /var/www$fastcgi_script_name;
        include        fastcgi_params;
    }
}

请记住将端口更改为您的php-fpm或您用来处理php代码的任何东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM