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. 请教一下,nginx的rewrite规则怎么写?

比如将 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

规则就是id除1000,如果小于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/

Nginx 一些常用的URL 重写方法的更多相关文章

  1. nginx 常用的 URL 重写方法

    转自:http://www.jbxue.com/article/4727.html Nginx中一些常用的URL 重写方法介绍,有需要的朋友可以参考下.url重写应该不陌生,不管是SEO URL 伪静 ...

  2. Nginx配置虚拟机,url重写,防盗链

    配置目录: ·     虚拟主机 ·     PHP支持 ·     URL重写 ·     防止盗链 ·     持续更新… 一.虚拟主机 1.创建 文件格式:{域名}.conf 具体如下: $ s ...

  3. 配置Nginx支持ThinkPHP的URL重写和PATHINFO

    ThinkPHP支持通过PATHINFO和URL rewrite的方式来提供友好的URL,只需要在配置文件中设置 'URL_MODEL' => 2 即可.在Apache下只需要开启mod_rew ...

  4. [转]让Nginx支持ThinkPHP的URL重写和PATHINFO

    From : http://www.jzxue.com/wangzhankaifa/php/201108/08-8396.html   ThinkPHP支持通过PATHINFO和URL rewrite ...

  5. 在phpstudy集成环境下的nginx服务器下配置url重写

    直接在对应的vhosts.conf配置文件的location / {}中添加以下内容: location / { index index.html index.htm index.php; #auto ...

  6. HttpServletRequest常用获取URL的方法

    1.request.getRequestURL() 返回的是完整的url,包括Http协议,端口号,servlet名字和映射路径,但它不包含请求参数.2.request.getRequestURI() ...

  7. thinkphp 3.2.3在nginx+php下的url重写配置经验

    环境:centos7.2+lnmp1.3(nginx+php7.0+mysql5.5) 进入服务器配置路径:cd /usr/local/nginx/conf/nginx.conf 修改nginx.co ...

  8. 【javaWeb】HttpServletRequest常用获取URL的方法

    1.request.getRequestURL() 返回的是完整的url,包括Http协议,端口号,servlet名字和映射路径,但它不包含请求参数. 2.request.getRequestURI( ...

  9. 很不错的NGINX URL重写实例

    转自:http://www.jbxue.com/article/2187.html 本文介绍nginx URL重写的相关知识,包括301重定向的内容等,希望对大家有所帮助. nginx rewrite ...

随机推荐

  1. 【转】手摸手,带你用vue撸后台 系列四(vueAdmin 一个极简的后台基础模板)

    前言 做这个 vueAdmin-template 的主要原因是: vue-element-admin 这个项目的初衷是一个vue的管理后台集成方案,把平时用到的一些组件或者经验分享给大家,同时它也在不 ...

  2. linux下常用的日志分析命令【转】

    形如下面这样的access.log日志内容: 211.123.23.133 – - [10/Dec/2010:09:31:17 +0800] “GET /query/trendxml/district ...

  3. HDOJ 1085 Holding Bin-Laden Captive!

    Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  4. linux下编译安装gcc5.1

    一.下载源代码:ftp://ftp.mirrorservice.org/sites/sourceware.org/pub/gcc/releases/gcc-5.1.0/gcc-5.1.0.tar.bz ...

  5. 什麼是 usb upstream port

    主機USB埠是定義為USB纜線的上行端(Upstream)或「A」接頭,即PC端.而裝置USB埠是定義為USB纜線的下行端(Downstream)或「B」接頭,即行動產品端. Reference ht ...

  6. Linux设备模型(热插拔、mdev 与 firmware)【转】

    转自:http://www.cnblogs.com/hnrainll/archive/2011/06/10/2077469.html 转自:http://blog.chinaunix.net/spac ...

  7. layui 的Tab选项卡

    http://www.layui.com/doc/element/tab.html <#--start--> <div class="layui-tab layui-tab ...

  8. Python实现简单的Web服务器 解析

    代码来源https://www.shiyanlou.com/courses/552,对它进行理解,注释 #-*- coding:utf-8 -*- import BaseHTTPServer clas ...

  9. 解决 GTK+/GNOME 3 环境下 Java Swing 程序使用本地 GTK+ 主题时菜单无边框 bug 的方法

    在 GTK+/GNOME 3 环境下采用默认的 Adwaita 主题时,Java Swing 程序如果使用本地 GTK+ 主题会出现菜单无边框的 bug,这个问题也可能在其他常用的 GTK+ 主题中出 ...

  10. AC日记——[ZJOI2006]物流运输 bzoj 1003

    1003 思路: 最短路+dp: 节点在a-b天里不能使用 那么我们准备每一组a-b求一条最短路,如果没有,则用极大值表示: cost[a,b]记录这个最短路: 然后,开始dp: dp[i]=min( ...