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 閱讀(15451) 評論(0)  編輯  收藏 所屬分類: nginx

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


          網站導航:
           

          常用鏈接

          留言簿(21)

          隨筆分類(265)

          隨筆檔案(242)

          相冊

          JAVA網站

          關注的Blog

          搜索

          •  

          積分與排名

          • 積分 - 914040
          • 排名 - 40

          最新評論

          主站蜘蛛池模板: 阿勒泰市| 昔阳县| 河津市| 宜城市| 七台河市| 军事| 沁阳市| 克山县| 石柱| 张家港市| 陇川县| 大同县| 泰和县| 湘潭市| 清镇市| 宣汉县| 加查县| 武功县| 永仁县| 错那县| 黔西县| 钦州市| 五指山市| 宣恩县| 休宁县| 广平县| 安宁市| 马鞍山市| 宜兴市| 拉孜县| 克什克腾旗| 广安市| 临城县| 岳阳市| 滦南县| 全南县| 衡阳市| 新丰县| 织金县| 桑日县| 巧家县|