php - Nginx 带问号匹配URL

查看:595
本文介绍了php - Nginx 带问号匹配URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

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

解决方案

按照你的意思,不应该是访问 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

这篇关于php - Nginx 带问号匹配URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆