您的位置:首页 > 运维架构 > Apache

第十五章 apache别名和重定向

2012-10-04 12:30 288 查看
版本V1.0

时间2012-10-04
版权GPL
作者itnihao 邮箱 itnihao@qq.com
博客 http://itnihao.blog.51cto.com
如需重新发行,请注明以上信息,谢谢合作

前言
本文档基于对apache的学习整理而成的笔记。本文档详细的记录了apache各种应用,以及一些个人的理解,如果偏差,请和我联系,以在下一个版本中进行更正。其中大部分文档均来自网络,感谢网络上各位朋友的分享,才有此文档的出现。其中本人对参考的部分网络文档进行适当的修改,以达到更好的参考效果。也希望各位积极的分享文档,为开源事业做出自己力所能及的贡献。
itnihao 2012年10月04日于成都

第十五章 apache别名和重定向

15.1 mod_alias模块

Mod_alias提供了4条指令用于别名的定义


Alias 将URL映射到你所定义的目录或文件上
Alias /web /data/app/web
普通别名定义
与Alias指令相同,但可使用正则表达式规则定义
AliasMatch AliasMatch ^/manual/(.*) /data/web/munual$1


ScriptAlias 与Alias类似,只是将所定义的目录识别为CGI脚本目录
ScriptsAlias /cgi-bin/ "/data/http-ssl/cgi-bin/"
CGI脚本目录的别名定义
允许使用正则表达式来定义CGI脚本目录
ScriptAliasMatch ScriptAliasMatch ^/cgi-bin/(.*) "/data/http-ssl/cgi-bin/$1"

15.2 重定向的定义

#wget -S --spider 192.168.16.30/a
Spider mode enabled. Check if remote file exists.
--2012-09-26 15:25:44-- http://192.168.16.30/a Connecting to 192.168.16.30:80... connected.
HTTP request sent, awaiting response...
HTTP/1.1 301 Moved Permanently #服务器发出地址移动的响应
Date: Wed, 03 Oct 2012 17:29:04 GMT
Server: Apache/2.4.2 (Unix)
Location: http://192.168.16.30/a/ Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=iso-8859-1
Location: http://192.168.16.30/a/ [following] #跳转到新地址
Spider mode enabled. Check if remote file exists.
--2012-09-26 15:25:44-- http://192.168.16.30/a/ #开始连接新地址
Connecting to 192.168.16.30:80... connected.
HTTP request sent, awaiting response...
HTTP/1.1 200 OK #连接成功
Date: Wed, 03 Oct 2012 17:29:04 GMT
Server: Apache/2.4.2 (Unix)
Last-Modified: Wed, 03 Oct 2012 11:56:44 GMT
ETag: "2d-4cb265528a700"
Accept-Ranges: bytes
Content-Length: 45
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html
Length: 45 [text/html]
Remote file exists and could contain further links,
but recursion is disabled -- not retrieving.
由于请求的是192.168.16.30/a,浏览器会当成一个文件,于是浏览器发出请求,但a在服务器中是一个目录,于是服务器并不会直接返回a目录的内容,而是通过重定向告诉浏览器是一个目录,当浏览器收到响应后会重新发送一个URL请求http://192.168.16.30/a/,之后服务器返回正确信息。
重写指令
1.RewriteEngine指令,开关on,off
RewriteEngine on
2.RewriteRule
RewriteRule Pattern substitution [flags]
(1)Pattern作用于当前请求的URL正则表达式,这个URL可能和用户请求的不同,因为可能已经被RewriteRule或是Alias指令修改过了。
(2)Substitution参数是当原始URL与Pattern相匹配时,用来替换的字符串
l 对Pattern的反引用($N),N的范围是0-9
RewriteRule ^/~/([^/]+)/?(.*) /u/$1/$2
l 对最后匹配的RewriteCond的反向引用(%N),N范围0-9
l 规则条件测试字符串(%{VARNAME})中的服务器变量,%{VARNAME}由Apache环境中的VARNAME的值进行填充
l 映射函数调用(${mapname:key|default}),用户可以通过${mapname:key|default}的形式来调用由RewriteMap定义的映射
(3)[flags]参数为可选参数

推荐阅读深入理解Apache的mod_rewrite

http://www.yeeyan.org/articles/view/jcky/59298

完整版见附件

本文出自 “itnihao的运维技术博客” 博客,请务必保留此出处http://itnihao.blog.51cto.com/1741976/1012154
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: