登录

php - Nginx 带问号匹配URL

本人现在有个需求是做seo优化,代码用的yii框架,现在要把部分原来的长链接改为短链接,对此现在有两个思路,一个是在PHP里面做301跳转,一个是在nginx配置文件做301,问题来了:我在nginx配置文件location里匹配不了?,打比方,原来的链接是xxx.bbb.ccc/index.php?r=pc/index/index ,现在使用xxx.bbb.ccc/index,请问怎么来匹配这个?

# PHP
为情所困 为情所困 2463 天前 935 次浏览

全部回复(1) 我要回复

  • 黄舟

    黄舟2017-07-01 09:14:11

    按照你的意思,不应该是访问 xxx.bbb.ccc/index -> xxx.bbb.ccc/index.php?r=pc/index/index 的吗?

    这样的话,没必要匹配到?的呀。

    直接写 rewrite 规则就可以了

    rewrite ^/index$ /index.php?r=pc/index/index last;

    如果是反向的那就用 if 判断好了

    如:

    server {
        server_name test.dev;
        location / {
            if ($request_uri ~ '/index.php\?r=pc/index/index') {
                return 301 http://test.dev/index;
            }
        }
    }

    测试结果:

    > curl -I 'http://test.dev/index.php?r=pc/index/index'
    
    HTTP/1.1 301 Moved Permanently
    Server: nginx/1.10.3
    Date: Fri, 30 Jun 2017 09:04:12 GMT
    Content-Type: text/html
    Content-Length: 185
    Connection: keep-alive
    Location: http://test.dev/index

    回复
    0
  • 取消 回复 发送