Apache 学习笔记 - mod_rewrite参考文档 (mod_rewrite reference documentation)

http://httpd.apache.org/docs/2.4/en/mod/mod_rewrite.html

Apache Module mod_rewrite

描述:提供基于规则的重写引擎,用于即时重写所请求的URL
模块标识符:rewrite_module
源文件:mod_rewrite.c

Summary
The mod_rewrite module uses a rule-based rewriting engine, based on a PCRE regular-expression parser, to rewrite requested URLs on the fly. By default, mod_rewrite maps a URL to a filesystem path. However, it can also be used to redirect one URL to another URL, or to invoke an internal proxy fetch.
该mod_rewrite模块使用基于规则的重写引擎(基于PCRE正则表达式解析器)即时重写所请求的URL。默认情况下,mod_rewrite将URL映射到文件系统路径。但是,它也可以用于将一个URL重定向到另一个URL,或调用内部代理提取。

mod_rewrite provides a flexible and powerful way to manipulate URLs using an unlimited number of rules. Each rule can have an unlimited number of attached rule conditions, to allow you to rewrite URL based on server variables, environment variables, HTTP headers, or time stamps.
mod_rewrite提供了一种灵活而强大的方式来使用无限数量的规则来操纵URL。每个规则可以具有无限数量的附加规则条件,以允许您根据服务器变量,环境变量,HTTP标头或时间戳重写URL。

mod_rewrite operates on the full URL path, including the path-info section. A rewrite rule can be invoked in httpd.conf or in .htaccess. The path generated by a rewrite rule can include a query string, or can lead to internal sub-processing, external request redirection, or internal proxy throughput.
mod_rewrite在完整的URL路径上运行,包括路径信息部分。重写规则可以在httpd.conf或中被调用 .htaccess。重写规则生成的路径可以包含查询字符串,也可以导致内部子处理,外部请求重定向或内部代理吞吐量。

Further details, discussion, and examples, are provided in the detailed mod_rewrite documentation.
进一步的细节,讨论和例子在 详细的mod_rewrite文档中提供。

日志(Logging)

mod_rewrite offers detailed logging of its actions at the trace1 to trace8 log levels. The log level can be set specifically for mod_rewrite using the LogLevel directive: Up to level debug, no actions are logged, while trace8 means that practically all actions are logged.
mod_rewrite提供了在trace1到trace8日志级别的详细日志记录。可以 使用LogLevel指令专门为mod_rewrite设置日志级别:达到最高级别时debug,不记录任何操作,而trace8意味着几乎所有的操作都被记录下来。

Using a high trace log level for mod_rewrite will slow down your Apache HTTP Server dramatically! Use a log level higher than trace2 only for debugging!
使用高跟踪日志级别mod_rewrite 会显着降低Apache HTTP Server的速度!使用高于trace2的日志级别仅用于调试!

Example

LogLevel alert rewrite:trace3

RewriteLog
Those familiar with earlier versions of mod_rewrite will no doubt be looking for the RewriteLog and RewriteLogLevel directives. This functionality has been completely replaced by the new per-module logging configuration mentioned above.
那些熟悉早期版本的 mod_rewrite无疑将寻找 RewriteLog和RewriteLogLevel 指令。此功能已被上述新的per-module日志记录配置完全取代。

To get just the mod_rewrite-specific log messages, pipe the log file through grep:
要获得mod_rewrite特定的日志消息,请通过grep:

tail -f error_log|fgrep '[rewrite:'

RewriteBase 指令(RewriteBase Directive)

描述: 设置每个目录重写的基本URL (Sets the base URL for per-directory rewrites)
语法: RewriteBase URL-path
默认: None
上下文(Context): directory, .htaccess
覆盖: FileInfo
模块: mod_rewrite的

The RewriteBase directive specifies the URL prefix to be used for per-directory (htaccess) RewriteRule directives that substitute a relative path.
该RewriteBase指令指定用于RewriteRule替换相对路径的每个目录(htaccess)指令的URL前缀 。

This directive is required when you use a relative path in a substitution in per-directory (htaccess) context unless any of the following conditions are true:
除非满足以下任一条件,否则在每个目录(htaccess)上下文中的替换中使用相对路径时,此指令是必需的:

  • The original request, and the substitution, are underneath the DocumentRoot (as opposed to reachable by other means, such as Alias).
    原始请求和替换位于DocumentRoot (而不是通过其他方式可访问的)之下 ,例如 Alias。

  • The filesystem path to the directory containing the RewriteRule, suffixed by the relative substitution is also valid as a URL path on the server (this is rare).
    包含后缀(通过相对替换后缀)的目录的文件系统路径 RewriteRule在服务器上也是有效的URL路径(这很少见)。

  • In Apache HTTP Server 2.4.16 and later, this directive may be omitted when the request is mapped via Alias or mod_userdir.
    在Apache HTTP Server 2.4.16及更高版本中,当通过Alias or 映射请求时,可以省略该指令 mod_userdir。

In the example below, RewriteBase is necessary to avoid rewriting to http://example.com/opt/myapp-1.2.3/welcome.html since the resource was not relative to the document root. This misconfiguration would normally cause the server to look for an “opt” directory under the document root.
在下面的示例中,RewriteBase必须避免重写到http://example.com/opt/myapp-1.2.3/welcome.html,因为资源与文档根目录不相关。这种错误配置通常会导致服务器在文档根目录下查找“opt”目录。

DocumentRoot "/var/www/example.com"
AliasMatch "^/myapp" "/opt/myapp-1.2.3"

    RewriteEngine On
    RewriteBase "/myapp/"
    RewriteRule "^index\.html$"  "welcome.html"

未完待续。。。

你可能感兴趣的:(服务器)