apache重写规则详解

摘要:# Various rewrite rules.<IfModule mod_rewrite.c> RewriteEngine on # If your site can be accessed both with and without the 'www.' prefix, you # can use one of the following settings to redire…php

# Various rewrite rules.

<IfModule mod_rewrite.c>

  RewriteEngine on



  # If your site can be accessed both with and without the 'www.' prefix, you

  # can use one of the following settings to redirect users to your preferred

  # URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option:

  #

  # To redirect all users to access the site WITH the 'www.' prefix,

  # (http://example.com/... will be redirected to http://www.example.com/...)

  # adapt and uncomment the following:

  # RewriteCond %{HTTP_HOST} ^example.com$ [NC]

  # RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

  #

  # To redirect all users to access the site WITHOUT the 'www.' prefix,

  # (http://www.example.com/... will be redirected to http://example.com/...)

  # uncomment and adapt the following:

  # RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]

  # RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]



  # Modify the RewriteBase if you are using Drupal in a subdirectory or in a

  # VirtualDocumentRoot and the rewrite rules are not working properly.

  # For example if your site is at http://example.com/drupal uncomment and

  # modify the following line:

  # RewriteBase /drupal

  #

  # If your site is running in a VirtualDocumentRoot at http://example.com/,

  # uncomment the following line:

  # RewriteBase /



  # Rewrite URLs of the form 'x' to the form 'index.php?q=x'.

  RewriteCond %{REQUEST_FILENAME} !-f

  RewriteCond %{REQUEST_FILENAME} !-d

  RewriteCond %{REQUEST_URI} !=/favicon.ico

  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

</IfModule>

 

RewriteEngine on
为重写引擎开关,若是设为off,则任何重写规则定义将不被应用,该开关的另外一好处就是若是为了临时拿掉重写规则,则改成off再重启动Apache便可,没必要将下面一条条的重写规则注释掉。html

 

 

RewriteCond 条件重写规则,当知足后面定义的条件后才会应用下面的重写规则。java

RewriteCond 的语法以下: web

RewriteCond  TestString CondPattern [flags] 正则表达式

TestString 服务器

TestString 是一个纯文本的字符串,可是也能够包含一些扩展的成分,这里就是经过 %{NAME_OF_VARIABLE} 引用的服务器变量。 app

服务器变量: 函数

REQUEST_FILENAME  是与请求相匹配的完整的本地文件系统的文件路径名。 测试

REQUEST_URI 是在HTTP请求行中所请求的资源(好比:"/index.html")。 ui

CondPattern

CondPattern是条件模式,即一个应用于当前TestString实例的正则表达式。TestString将被首先计算,而后再与CondPattern匹配。

好比: RewriteCond %{REQUEST_URI} !=/favicon.ico  的意思就是请求的地址不是 /favicon.ico 才触发下面的地址重写;

! (惊叹号)来指定不匹配。
'-d'(目录)  将TestString视为一个路径名并测试它是否为一个存在的目录。
'-f'(常规文件)  将TestString视为一个路径名并测试它是否为一个存在的常规文件。

因此: RewriteCond %{REQUEST_FILENAME} !-f  的意思为, 请求的文件并不存在时触发后面的地址重写;

RewriteCond %{REQUEST_FILENAME} !-d  的意思为: 请求的目录并不存在时,触发后面的地址重写;

更多这个参数的写法能够参看: http://www.52web.com/52article/?view-258.html

[flags]

咱们还能够在CondPattern以后追加特殊的标记[flags]做为RewriteCond指令的第三个参数。flags是一个以逗号分隔的如下标记的列表:

'nocase|NC'(忽略大小写)
它使测试忽略大小写,扩展后的TestString和CondPattern中'A-Z' 和'a-z'是没有区别的。此标记仅用于TestString和CondPattern的比较,而对文件系统和子请求的检查不起做用。
'ornext|OR'(或下一条件)
它以OR方式组合若干规则的条件,而不是隐含的AND。典型的例子以下:

 

 

RewriteRule 具体的重写策略:

RewriteRule的语法如后:  RewriteRule Pattern Substitution [flags]

Pattern

对Apache1.2及之后的版本,模板(Pattern)是一个POSIX正则式,用以匹配当前的URL。当前的URL不必定是最初提交的URL,由于可能用一些规则在此规则前已经对URL进行了处理。

这里是 ^(.*)$ ,意味着一行任何的地址请求;

Substitution

当匹配成功后,Substitution会被用来替换相应的匹配。

这里的 RewriteRule ^(.*)$ index.php?q=$1  意味着, 若是原先请求的是 /aaa/ddd  重写后地址变成 index.php?q=/aaa/ddd ;

它除了能够是普通的字符串之外,还能够包括:
1. $N,引用RewriteRule模板中匹配的相关字串,N表示序号,N=0..9
2. %N,引用最后一个RewriteCond模板中匹配的数据,N表示序号
3. %{VARNAME},服务器变量
4. ${mapname:key|default},映射函数调用。

更多信息能够参看:http://www.ixdba.net/article/ae/1443.html

[flags]

做为RewriteRule指令的第三个参数。 Flags是一个包含以逗号分隔的标记的列表。

这里的 L,QSA 分别是以下意思:

'last|L' (最后一个规则 last)
当即中止重写操做,并再也不应用其余重写规则。 它对应于Perl中的last命令或C语言中的break命令。 这个标记能够阻止当前已被重写的URL为其后继的规则所重写。 举例,使用它能够重写根路径的URL('/')为实际存在的URL, 好比, '/e/www/'.

'qsappend|QSA' (追加请求串 query string append)
此标记强制重写引擎在已有的替换串中追加一个请求串,而不是简单的替换。 若是须要经过重写规则在请求串中增长信息,就可使用这个标记。

好比说我要把 /user/ghj1976 重定向到/user.php?uid=ghj1976 那么个人规则就须这样:
RewriteRule ^user/([^/]+)$ ^/user.php?uid=$2 [L]

可是若是咱们但愿 /user/ghj1976?key=java  重定向到  /user.php?uid=ghj1976&key=java

写成 RewriteRule ^user/([^/]+)$ ^/user.php?uid=$2 [QSA,L]  这样增长个 QSA 就能够了。

若是没有QSA,就会丢掉咱们额外传的这个参数key=java。

相关文章
相关标签/搜索