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. 模板jinja2常用方法

    http://docs.jinkan.org/docs/jinja2/ 摘自 http://www.pythontip.com/blog/post/5455/ 数学运算       +, -, *,  ...

  2. poj 2836 Rectangular Covering

    Rectangular Covering Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2776   Accepted: 7 ...

  3. linux服务器端口netstat

    netstat命令各个参数说明如下: -t : 指明显示TCP端口 -u : 指明显示UDP端口 -l : 仅显示监听套接字(所谓套接字就是使应用程序能够读写与收发通讯协议(protocol)与资料的 ...

  4. 博客移至CSDN

    CSDN博客地址:http://blog.csdn.net/pilihaotian 博客园看心情更新.

  5. sql2008安装时 重新启动计算机 失败

    原文发布时间为:2010-11-02 -- 来源于本人的百度文章 [由搬家工具导入] sql2008安装时 重新启动计算机 失败解决方法:regedit 运行 打开注册表,找到HKEY_LOCAL_M ...

  6. nvarchar与varchar的区别

    原文发布时间为:2009-08-05 -- 来源于本人的百度文章 [由搬家工具导入] 1、通俗一點就是varchar適合輸入英文和數字,nvarchar一般用做中文或其它語言的輸入,這樣到別的語系不會 ...

  7. JQuery基础 学习的一些例子以及手册

    原文发布时间为:2009-12-23 -- 来源于本人的百度文章 [由搬家工具导入] 不多说,直接下载。。。 下载:http://www.xmaspx.com/Services/FileAttachm ...

  8. 使用Powerdesigner生成设计的数据表(一张或多张)的测试数据

    设计表完成以后,我们需要生成一些测试数据,可以直接更新到数据库中,下面我们就来试试: 第一步:建立需要的Profiles测试文件,[Model]--[Test Data Profiles],如图所示: ...

  9. Python环境安装与配置

    1.官网下载:https://www.python.org/选择不同的版本 2.进入运行:使用pip安装selenium 3.设置pip的环境变量 4.安装后使用pip(一个Python包管理工具)安 ...

  10. Algorithm | Vector

    因为平常用的话只是vector的一些非常简单的功能,基本上把它当数组来用,现在也只是把这一部分写了一些. template<class T> class XVector { public: ...