Nginx 一些常用的URL 重寫方法

          url重寫應該不陌生,不管是SEO URL 偽靜態的需要,還是在非常流行的wordpress里,重寫無處不在。

           

          1. Apache 的寫法

          RewriteCond  %{HTTP_HOST}  nginx.org
          RewriteRule  (.*)  http://www.nginx.org$1


          Nginx 可以對應寫成:


          server {
              listen       80;
              server_name  www.nginx.org  nginx.org;
              if ($http_host = nginx.org) {
                  rewrite  (.*)  http://www.nginx.org$1;
              }
              ...
          }

          Nginx 作者更建議的方法是:

          server {
              listen       80;
              server_name  nginx.org;
              rewrite   ^  http://www.nginx.org$request_uri?;
          }

          server {
              listen       80;
              server_name  www.nginx.org;
              ...
          }

           

          F&Q

          1. 請教一下,nginxrewrite規則怎么寫?

          比如將 http://http://www.oschina.net/222.html rewrite http://http://www.oschina.net/222.htm

           

          location ~ .*\.(html)$

                    {

                     rewrite ^(.*)\.html  $1.htm permanent;

                   }

           

          2. 下面url要怎么寫rewrite?

          www.aaa.com/search/?wd=搜索內容  ==> www.aaa.com/searchpage?keyword=搜索內容


          location ~ ^/search/ { 

                      rewrite (.*) /searchpage$1 ; 

           

          3. 請求的url如下 /item/12345/index.html 重定向到/item/12/12345/index.html

          規則就是id1000,如果小于id小于1000,則為/item/0/id/index.html

          不知道說清楚沒有,這個rewrite規則該怎么寫啊?


          剛看了文檔,似乎可以

          http://wiki.nginx.org/HttpRewriteModule

          /photos/123456  -> /path/to/photos/12/1234/123456.png

          rewrite  "/photos/([0-9] {2})([0-9] {2})([0-9] {2})" /path/to/photos/$1/$1$2/$1$2$3.png;

           

          備注:

           

          有一個工具是apache htaccess 文件轉 nginx rewrite:

          http://www.anilcetin.com/convert-apache-htaccess-to-nginx/

           

          posted on 2011-12-14 17:31 -274°C 閱讀(15463) 評論(0)  編輯  收藏 所屬分類: nginx

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          常用鏈接

          留言簿(21)

          隨筆分類(265)

          隨筆檔案(242)

          相冊

          JAVA網站

          關注的Blog

          搜索

          •  

          積分與排名

          • 積分 - 916098
          • 排名 - 40

          最新評論

          主站蜘蛛池模板: 台安县| 镇巴县| 大理市| 游戏| 广东省| 奉贤区| 洛浦县| 博爱县| 莎车县| 商河县| 广宁县| 晴隆县| 西贡区| 屏东市| 彰化县| 山阳县| 东莞市| 汝城县| 石泉县| 那坡县| 惠州市| 抚顺县| 轮台县| 樟树市| 出国| 望谟县| 宝应县| 临西县| 梧州市| 无棣县| 三江| 清水河县| 丹寨县| 甘德县| 兴化市| 三门县| 大冶市| 上思县| 万盛区| 南充市| 水城县|