访问项目地址:http://192.168.254.100/ecshop

某个商品的 URL:http://192.168.254.100/ecshop/goods.php?id=3

现在需要实现把以上 URL 改写成 http://192.168.254.100/ecshop/goods-3.html(ecshop 支持的简单重写模式)

此时访问 http://192.168.254.100/ecshop/goods-3.html 显示 404:

编辑 nginx 配置文件 nginx.conf:

[root@localhost nginx]# vim conf/nginx.conf

在 server 段中添加一个 location:

        location /ecshop {
root html; #可以在server中统一配置
rewrite "goods-(\d{1,7})\.html" /ecshop/goods.php?id=$; #goods的id有1-7位
}

注意:在进行 Rewrite 重写时,如果正则表达式中含有 {},那么整个正则表达式就要用引号 "" 包起来。

也就是说如果按照以下的写法,正则表达式不需要用引号包起来:

        location /ecshop {
root html; #可以在server中统一配置
rewrite goods-(\d+)\.html /ecshop/goods.php?id=$; # $1 反向引用
}

平滑重启 nginx。

此时访问 http://192.168.254.100/ecshop/goods-3.html:

重写成功。

登录后台:http://192.168.254.100/ecshop/admin/privilege.php?act=login

商店设置 -- 基本设置 -- URL 重写 -- 简单重写 -- 确定

此时再来到首页,任意点击一款商品,URL 的形式都自动变成了 http://192.168.254.100/ecshop/goods-xxx.html

但是由于在后台设置了简单重写,此时点击任意一篇文章都会出现 404:

解决方案:

编辑 nginx 配置文件 nginx.conf

[root@localhost nginx]# vim conf/nginx.conf

修改 location:

        location /ecshop {
root html; #可以在server中统一配置
rewrite goods-(\d+)\.html /ecshop/goods.php?id=$; # $1反向引用
rewrite article-(\d+)\.html /ecshop/article.php?id=$;
}

另外还有其他例如栏目、分页等 按照相同的规则进行重写

例如 首页 -- 手机类型 -- GSM 手机 -- 诺基亚 的 URL (禁用 URL 重写时)为

http://192.168.254.100/ecshop/category.php?id=3&brand=1&price_min=200&price_max=1700&filter_attr=167.229.202.199&page=2&sort=good_id&order-DESC

启用简单重写后该地址变为:http://192.168.254.100/ecshop/category-3-b1-min200-max1700-attr167.229.202.199-2-goods_id-DESC.html

b 代表 brand

min 代表 price_min

max 代表 price_max

attr 代表 filter_attr

修改 location:

        location /ecshop {
root html; #可以在server中统一配置
rewrite goods-(\d+)\.html /ecshop/goods.php?id=$; # $1反向引用
rewrite article-(\d+)\.html /ecshop/article.php?id=$;
rewrite category-(\d+)-b(\d+)-min(\d+)-max(\d+)-attr([\d\.]+)\.html /ecshop/category.php?id=$&brand=$&price_min=$&price_max=$4filter_attr=$;
rewrite category-(\d+)-b(\d+)-min(\d+)-max(\d+)-attr([\d\.]+)-(\d+)-(\w+)-(\w+)\.html /ecshop/category.php?id=$&brand=$&price_min=$&price_max=$4filter_attr=$&page=$&sort=$&order=$;
}

平滑重启 nginx。

再如:

http://192.168.254.100/ecshop/category-1-b0.html

修改 location

     location /ecshop {
root html; #可以在server中统一配置
rewrite goods-(\d+)\.html /ecshop/goods.php?id=$; # $1反向引用
rewrite article-(\d+)\.html /ecshop/article.php?id=$;
rewrite category-(\d+)-b(\d+)\.html /ecshop/category.php?id=$&brand=$;
rewrite category-(\d+)-b(\d+)-min(\d+)-max(\d+)-attr([\d\.]+)\.html /ecshop/category.php?id=$&brand=$&price_min=$&price_max=$4filter_attr=$;
rewrite category-(\d+)-b(\d+)-min(\d+)-max(\d+)-attr([\d\.]+)-(\d+)-(\w+)-(\w+)\.html /ecshop/category.php?id=$&brand=$&price_min=$&price_max=$4filter_attr=$&page=$&sort=$&order=$;
}

如果设置复杂重写:

即在一些 URL 中,把名称(分类名称、产品名称)也加入到了 URL 中。

修改 nginx.conf:

        location /ecshop {
root html; #可以在server中统一配置
rewrite goods-(\d+)\.html /ecshop/goods.php?id=$; # $1反向引用
rewrite article-(\d+)\.html /ecshop/article.php?id=$;
rewrite category-(\d+)-b(\d+)-.*?\.html /ecshop/category.php?id=$&brand=$;
rewrite category-(\d+)-b(\d+)-min(\d+)-max(\d+)-attr([\d\.]+)\.html /ecshop/category.php?id=$&brand=$&price_min=$&price_max=$4filter_attr=$;
rewrite category-(\d+)-b(\d+)-min(\d+)-max(\d+)-attr([\d\.]+)-(\d+)-(\w+)-(\w+)\.html /ecshop/category.php?id=$&brand=$&price_min=$&price_max=$4filter_attr=$&page=$&sort=$&order=$;
}

修改了第 3 条 Rewrite

注意:? 表示懒惰匹配

此时访问 http://192.168.254.100/ecshop/category-2-b0-CDMA%E6%89%8B%E6%9C%BA.html:

其他的 URL 以此类推。

注意:Rewrite 应该按照由繁到简的顺序写,否则翻页或者选择具体型号时会出现,匹配到前面的类别时就不往下进行匹配的结果:

        location /ecshop {
root html; #可以在server中统一配置
rewrite goods-(\d+)\.html /ecshop/goods.php?id=$; # $1反向引用
rewrite article-(\d+)\.html /ecshop/article.php?id=$;
rewrite category-(\d+)-b(\d+)-min(\d+)-max(\d+)-attr([\d\.]+)-(\d+)-(\w+)-(\w+).*\.html /ecshop/category.php?id=$&brand=$&price_min=$&price_max=$4filter_attr=$&page=$&sort=$&order=$;
rewrite category-(\d+)-b(\d+)-min(\d+)-max(\d+)-attr([\d\.]+).*\.html /ecshop/category.php?id=$&brand=$&price_min=$&price_max=$4filter_attr=$;
rewrite category-(\d+)-b(\d+).*\.html /ecshop/category.php?id=$&brand=$;
}

最后,为了只输入 http://192.168.254.100/ecshop/ 而不需要输入 index.php 便能访问网站,在 location /ecshop 段的末尾添加 index index.php:

        location /ecshop {
root html; #可以在server中统一配置
rewrite goods-(\d+)\.html /ecshop/goods.php?id=$; # $1反向引用
rewrite article-(\d+)\.html /ecshop/article.php?id=$;
rewrite category-(\d+)-b(\d+)-min(\d+)-max(\d+)-attr([\d\.]+)-(\d+)-(\w+)-(\w+).*\.html /ecshop/category.php?id=$&brand=$&price_min=$&price_max=$4filter_attr=$&page=$&sort=$&order=$;
rewrite category-(\d+)-b(\d+)-min(\d+)-max(\d+)-attr([\d\.]+).*\.html /ecshop/category.php?id=$&brand=$&price_min=$&price_max=$4filter_attr=$;
rewrite category-(\d+)-b(\d+).*\.html /ecshop/category.php?id=$&brand=$;
index index.php;
}

图片不显示是因为 location ~ image 段 把图片的根目录指向到了 /var/www 目录下,注释即可:

        location ~ image {
# root /var/www/;
# expires 1d;
# index index.html;
}

此时访问 http://192.168.254.100/ecshop:

Nginx 笔记与总结(12)Nginx URL Rewrite 实例(ecshop)的更多相关文章

  1. Nginx 笔记(四)nginx 原理与优化参数配置 与 nginx 搭建高可用集群

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.nginx 原理与优化参数配置 ​ ​ master-workers 的机制的好处 首先,对于每个 ...

  2. Nginx 笔记(二)nginx常用的命令和配置文件

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 1.nginx常用的命令 (1)启动命令 在/usr/local/nginx/sbin 目录下执行 ./ ...

  3. Nginx 笔记(一)nginx简介与安装

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) Nginx 简介: 1.介绍 nginx 的应用场景和具体可以做什么事情 2.介绍什么是反向代理 3.介 ...

  4. Nginx 笔记与总结(9)rewrite 重写规则

    重写的规则可以放在 serverer 里,也可以放在 location 里. rewrite 规则: 常用的命令有 ① if(条件){} 设定条件,再进行重写 if 语法: if 空格 (条件){   ...

  5. Nginx 笔记(三)nginx 配置实例 - 反向代理、负载均衡、动静分离

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.反向代理 反向代理准备工作: (1)在 liunx 系统安装 tomcat,使用默认端口 8080 ...

  6. Nginx笔记总结十九:nginx + fancy实现漂亮的索引目录

    编译:./configure --prefix=/usr/local/nginx --add-module=../ngx-fancyindex-master 配置: location / { fanc ...

  7. Nginx笔记总结十八:nginx统计响应的http状态码信息(ngx-http-status-code-counter)

    编译:./configure --prefix=/usr/local/nginx --add-module=../ngx_http_status_code_counter-master make &a ...

  8. Nginx笔记总结十六:nginx优化指南

    1.高层的配置 worker_processes 定义了nginx对外提供web服务时的worker进程数 worker_rlimit_nofile 更改worker进程最大打开文件数量限制,如果没有 ...

  9. Nginx笔记总结十五:nginx+keepalive+proxy_cache配置高可用nginx集群和高速缓存

    nginx编译 wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz ./configure --prefix=/usr/loca ...

随机推荐

  1. Linux下循环、选择、顺序操作

    1.Shell脚本数组操作小结 http://www.jb51.net/article/52382.htm Linux Shell在编程方面比Windows批处理强大很多,无论是在循环.运算. bas ...

  2. Ant基本使用指南

    近期碰到了其他人在讨论这个ant,已经很多人在使用,故对他进行收集资料进了解,以便方便去使用.同时,在学习struts+spring+hibernate,尤其是Appfuse的过程中大量涉及到ant的 ...

  3. Set和Map

    Set和Map

  4. MVC学习笔记---MVC的处理管线

    漫步ASP.NET MVC的处理管线   ASP.NET MVC从诞生到现在已经好几个年头了,这个框架提供一种全新的开发模式,更符合web开发本质.你可以很好的使用以及个性化和扩展这个框架,但这需要你 ...

  5. 在Android中让Preference的宽度占满整个屏幕的宽度

    今天遇到一个问题,需要修改Preference的宽度,让其与屏幕宽度一致.搞了一上午. 终于发现Preference的这个尺寸是在PreferenceFrameLayout中设置的.通过下面这段代码, ...

  6. javascript实现的图数据结构的广度优先 搜索(Breadth-First Search,BFS)和深度优先搜索(Depth-First Search,DFS)

    最后一例,搞得快.三天之内走了一次.. 下一步,面象对像的javascript编程. function Dictionary(){ var items = {}; this.has = functio ...

  7. Java Hour 9

    有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 本文作者Java 现经验约为7 Hour,请各位不吝赐教. Hour 9 方法 ...

  8. 深入理解 KVC\KVO 实现机制 — KVO

    KVC和KVO都属于键值编程而且底层实现机制都是isa-swizzing,所以本来想放在一起讲的.但是篇幅有限所以就分成了两篇博文. KVC实现机制传送门 KVO概述 键值观察Key-Value-Ob ...

  9. SQL分布式查询、跨数据库查询

    --[方法1]连接服务器方法 --step1 创建链接服务器  exec sp_addlinkedserver     'srv_lnk','','SQLOLEDB', 'ip地址'  exec sp ...

  10. php二位数组合并

    转自:http://www.cnblogs.com/losesea/archive/2013/06/14/3134900.html 题目:有以下2个二维数组 1$a=Array(0 => Arr ...