php - Nginx 带问号匹配URL
为情所困
为情所困 2017-07-01 09:12:11
0
1
992

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

为情所困
为情所困

모든 응답(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
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!