快捷搜索:   服务器  安全  linux 安全  MYSQL  dedecms

mod_rewrite模块详解(3)

  'skip|S=num' (跳过后继的规则 skip)

  此标记强制重写引擎跳过当前匹配规则后继的num个规则。 它可以实现一个伪if-then-else的构造: 最后一个规则是then从句,而被跳过的skip=N个规则是else从句. (它和'chain|C'标记是不同的!)

  'env|E=VAR:VAL' (设置环境变量 environment variable)

  此标记使环境变量VAR的值为VAL, VAL可以包含可扩展的反向引用的正则表达式$N和%N。 此标记可以多次使用以设置多个变量。 这些变量可以在其后许多情况下被间接引用,但通常是在XSSI (via <!--#echo var="VAR"-->) or CGI (如 $ENV{'VAR'})中, 也可以在后继的RewriteCond指令的pattern中通过%{ENV:VAR}作引用。 使用它可以从URL中剥离并记住一些信息。

  'cookie|CO=NAME:VAL:domain[:lifetime[:path]]' (设置cookie)

  它在客户端浏览器上设置一个cookie。 cookie的名称是NAME,其值是VAL。 domain字段是该cookie的域,比如'.apache.org', 可选的lifetime是cookie生命期的分钟数, 可选的path是cookie的路径。

  注意

  绝不要忘记,在服务器级配置文件中,Pattern是作用于整个URL的。 但是在目录级配置文件中, (一般总是和特定目录名称相同的)目录前缀会在pattern匹配时被自动删除, 而又在替换完毕后自动被加上。此特性对很多种重写是必须的,因为, 如果没有这个剥离前缀的动作,就必须与其父目录去匹配,而这并不总是可行的。

  但是有一个例外: 如果替换串以``http://''开头, 则不会附加目录前缀, 而是强制产生一个外部重定向,或者(如果使用了P标记)是一个代理操作!

  注意

  为了对目录级配置启用重写引擎,你必须在这些文件中设置``RewriteEngine On'', 并且打开``Options FollowSymLinks'。 如果管理员对用户目录禁用了FollowSymLinks, 则无法使用重写引擎。这个限制是为了安全而设置的。

  以下是所有可能的替换组合及其含义:

  在服务器级配置中(httpd.conf)

  ,对这样一个请求 ``GET /somepath/pathinfo'':

  Given Rule Resulting Substitution

  ---------------------------------------------- ----------------------------------

  ^/somepath(.*) otherpath$1 not supported, because invalid!

  ^/somepath(.*) otherpath$1 [R] not supported, because invalid!

  ^/somepath(.*) otherpath$1 [P] not supported, because invalid!

  ---------------------------------------------- ----------------------------------

  ^/somepath(.*) /otherpath$1 /otherpath/pathinfo

  ^/somepath(.*) /otherpath$1 [R] http://thishost/otherpath/pathinfo 

  via external redirection

  ^/somepath(.*) /otherpath$1 [P] not supported, because silly!

  ---------------------------------------------- ----------------------------------

  ^/somepath(.*) http://thishost/otherpath$1 /otherpath/pathinfo

  ^/somepath(.*) http://thishost/otherpath$1 [R] http://thishost/otherpath/pathinfo 

  via external redirection

  ^/somepath(.*) http://thishost/otherpath$1 [P] not supported, because silly!

  ---------------------------------------------- ----------------------------------

  ^/somepath(.*) http://otherhost/otherpath$1 http://otherhost/otherpath/pathinfo 

  via external redirection

  ^/somepath(.*) http://otherhost/otherpath$1 [R] http://otherhost/otherpath/pathinfo 

  via external redirection

  (the [R] flag is redundant)

  ^/somepath(.*) http://otherhost/otherpath$1 [P] http://otherhost/otherpath/pathinfo 

  via internal proxy

  在/somepath的目录级配置中

  (即, 目录/physical/path/to/somepath的.htaccess文件中包含 RewriteBase /somepath)

  对这样一个请求``GET /somepath/localpath/pathinfo'':

  Given Rule Resulting Substitution

  ---------------------------------------------- ----------------------------------

  ^localpath(.*) otherpath$1 /somepath/otherpath/pathinfo

  ^localpath(.*) otherpath$1 [R] http://thishost/somepath/otherpath/pathinfo 

  via external redirection

  ^localpath(.*) otherpath$1 [P] not supported, because silly!

  ---------------------------------------------- ----------------------------------

  ^localpath(.*) /otherpath$1 /otherpath/pathinfo

  ^localpath(.*) /otherpath$1 [R] http://thishost/otherpath/pathinfo 

  via external redirection

  ^localpath(.*) /otherpath$1 [P] not supported, because silly!

  ---------------------------------------------- ----------------------------------

  ^localpath(.*) http://thishost/otherpath$1 /otherpath/pathinfo

  ^localpath(.*) http://thishost/otherpath$1 [R] http://thishost/otherpath/pathinfo 

  via external redirection

  ^localpath(.*) http://thishost/otherpath$1 [P] not supported, because silly!

  ---------------------------------------------- ----------------------------------

  ^localpath(.*) http://otherhost/otherpath$1 http://otherhost/otherpath/pathinfo 

  via external redirection

  ^localpath(.*) http://otherhost/otherpath$1 [R] http://otherhost/otherpath/pathinfo 

  via external redirection

  (the [R] flag is redundant)

  ^localpath(.*) http://otherhost/otherpath$1 [P] http://otherhost/otherpath/pathinfo 

  via internal proxy

  举例:

  要重写这种形式的URL

  / Language /~ Realname /.../ File

  为

  /u/ Username /.../ File . Language

  可以把这样的对应关系保存在/path/to/file/map.txt映射文件中, 此后,只要在Apache服务器配置文件中增加下列行,即可:

  RewriteLog /path/to/file/rewrite.log

  RewriteMap real-to-user txt:/path/to/file/map.txt

  RewriteRule ^/([^/]+)/~([^/]+)/(.*)$ /u/${real-to-user:$2|nobody}/$3.$

顶(0)
踩(0)

您可能还会对下面的文章感兴趣:

最新评论