Apache Drupal URL重写

在 drupal 跟目录下有个 .htaccess 文件, 这个文件中就有URL地址重写的配置信息,配置信息如下:

# 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即可,不必将下面一条条的重写规则注释掉。

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

RewriteCond 的语法如下:

RewriteCond  TestString CondPattern [flags]

TestString

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

服务器变量:

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

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

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。

更多可以参看: http://space.itpub.net/16555225/viewspace-495945

原文地址:https://www.cnblogs.com/ghj1976/p/1780844.html