php - Nginx 帶問號匹配URL
为情所困
为情所困 2017-07-01 09:12:11
0
1
993

本人現在有個需求是做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學習者快速成長!