Nginx 带问号匹配URL

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

阅读 5.4k
1 个回答

按照你的意思,不应该是访问 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
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题