tset123 发表于 2017-12-24 11:34:52

RewriteMap(apache)

  最近在工作中发现一个陌生的语法,apache服务器站点rewrite配置文件里的,开始还以为是apache的一种新语法,以这个词网上搜索,没搜到相关文章,跟老同事请教了一下,说这个是RewriteMap,之前也不太了解这个指令,今天特意网上查了下,发现不错,参考地址:https://httpd.apache.org/docs/current/rewrite/rewritemap.html ,http://blog.csdn.net/phphot/article/details/4049242 整理如下
  指令介绍截图如下

  根据说明,我们知道这个指令相当于定义了一个可扩展的方法用于RewriteCond和RewriteRule,实现类似替换的功能(传入一个字符串,返回一个字符串或空串)。
  定义一个扩展方法的语法为
  RewriteMap MapName MapType:MapSource
  MapName为方法名,可以是任意的字符串(避免使用服务器保留字,应该会有问题)
  MapType是方法的类型,可选类型有txt,rnd,dbm,int,prg,dbd,fastdbd
  MapSource是不同类型下的执行方法,类似于编程语言里的函数的方法体,暂时先这样理解,后面会具体描述
  扩展方法的使用
    ${ MapName : LookupKey }
    ${ MapName : LookupKey | DefaultValue }
  MapName为扩展方法定义时的方法名
  LookupKey是传入方法的参数
  DefaultValue为默认值,如果方法返回空串,则返回默认值
  方法类型简要信息如下

  txt 文本类型
  如果方法类型为txt,则MapSource为一个文本文件,文件文件的内容是每行以空格为分割符的键值对,可以有注释,注释以#开头。查询到的键值对会被httpd缓存,除非服务器重启或者是MapSource文本文件修改时间变更

  rnd 文本类型扩展(随机)
  如果方法类型为rnd,类似于txt类型,不过文本文件中键值对中的值可以有多个,用|分割,匹配到键之后,随机返回某一个值

  int 内部方法
  如果方法类型为int,表明使用内部方法,大小写转化,编码特殊字符,解码特殊字符

  dbm dbm hash file
  如果方法类型为dbm,表明使用hash文件,和文本文件的区别在于有索引,更高效且也支持缓存查询到的键。使用的hash文件可以由文本文件通过httxt2dbm工具生成,生成的hash文件有sdbm,gdbm,ndbm,db四种类型

  dbd 或者 fastdbd
  如果方法类型为dbd或fastdbd,表明使用sql查询。使用此种类型需要确保数据库模块(mod_dbd)被正确配置。sql查询返回可能不止一行,如果是多行的情况,则随机使用一行。dbd和fastdbd两者的区别在于dbd每次请求都会去查询数据库,而fastdbd会缓存数据库查询,除非服务器重启。fastdbd更高效更快

  prg 自定义脚本
  如果方法类型为prg,表明使用自定义脚本处理,传入脚本的值通过stdin接收,返回的值通过stdout返回

  工作中的使用场景是url的301跳转,通过dbm
  配置如下
  RewriteMap map_301 dbm=db:/xxx/rewrite_mapping_301.db
  RewriteCond ${map_301:%{REQUEST_URI}}!^$
  RewriteRule ^/(.*)${map_301:%{REQUEST_URI}}
  原始文本文件类似
  /test-brandstore/ /test-fashionfriends/
  /test-nature-me/ /test-douglas/
  /test-reifenchef/ /test-fritzreifen/
  /test-personalgifts/ /test-yoursurprise/
  /test-entertain/ /test-telekom/
  /test-posterjack/ /test-posterxxl/
  /test-d-living/ /test-mytime/
  /test-klick/ /test-whitewall/
  /test-myprinting/ /test-snapfish/
  /test-papershaker/ /test-photobox/
  /test-getmobile/ /test-sparhandy/
  /test-parfumidee/ /test-geschenkidee-ch/
  /test-ebookers-at/ /test-expedia-at/
  /test-rs-components/ /test-rs-online/
  /test-jack-und-jones/ /test-jack-and-jones/
  /test-nded/ /test-nded-de/
  /test-surfstitch/ /test-surfdome/
  /test-hochzeitsgeschenke/ /test-geschenke24/
  /test-baby-butt/ /test-kinderbutt/
  /test-parfumdeal/ /test-yatego/
  /test-gimahhot/ /test-yatego/
  最后放一张httxt2dbm工具的使用方法截图,方便查阅
页: [1]
查看完整版本: RewriteMap(apache)