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

Apache rewrite规则的配置过程

2014-10-31 17:01 393 查看
Apache rewrite规则的配置过程服务器端:192.168.10.201 (RHEL 5.9)
客户端:192.168.10.40(Linux)
192.168.10.246 (windows xp)

1.安装Apache
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# ls /etc/httpd/modules/mod_rewrite.so
mod_rewrite.so ##检查确定有此模块

vim /etc/httpd/conf/httpd.conf
LoadModule rewrite_module modules/mod_rewrite.so ##检查确定有此行
2.创建基于域名的虚拟主机
[root@localhost ~]# vim /etc/httpd/conf.d/vhosts.conf
<VirtualHost *:80>
ServerAdmin hunk.test.com
DocumentRoot "/var/www/html/hunk"
ServerName 192.168.10.201
RewriteEngine on ##打开rewirte功能
Include conf.d/hunk-rewrite.conf
</VirtualHost>

3.写rewrite规则
[root@localhost ~]# vim /etc/httpd/conf.d/hunk-rewrite.conf
RewriteCond %{HTTP_HOST} hunk.test.com [NC]
RewriteRule ^(.*)/index.html$ http://hunk.test.com/test.html [L,R=302]

4.在/var/www/html/hunk 添加测试页面。
[root@localhost ~]# vim /var/www/html/hunk/index.html
<html>
<head>
<title>
test page
</title>
</head>
<body >
<p align="center">
<font size="32px" color="red">
this is test page !page jump failure!
</font>
</p>
</body>
</html>

[root@localhost ~]# vim /var/www/html/hunk/hunk.html
<html>
<head>
<title>
test page
</title>
</head>
<body >
<p align="center">
<font size="32px" color="red">
this is test page !page jump success!
</font>
</p>
</body>
</html>

客户端设置
5.修改hosts文件
xp:C:\Windows\System32\drivers\etc\hosts
192.168.10.246 hunk.test.com ##添加此行
访问hunk.test.com/index.html
this is test page !page jump success!


linux :
[root@localhost ~]# vim /etc/hosts
192.168.10.40 hunk.test.com
[root@localhost ~]# elinks --dump hunk.test.com/index.html
this is test page !page jump success!




注意:
1.书写rewrite需要知道正则表达式的知识。
在RewriteRule ^(.*)/index.html$ http://hunk.test.com/test.html [L,R=302]中,^表示以某某开头,$表示以某某结尾, .(点)表示匹配任意单个字符,*表示匹配前面的子表达式零次或多次(大于等于0次)
2.这里搭建的是基于域名的虚拟主机的web服务器,所以下面两行代码是在/etc/httpd/conf.d/vhosts.conf书写。说明是此服务器也就是hunk.test.com 打开rewrite功能,并应用规则。

RewriteEngine on ##打开rewirte功能
Include conf.d/hunk-rewrite.conf
如果不是基于虚拟主机的web服务器,上面两行需要在/etc/httpd/conf/httpd.conf书写。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Linux Apache rewrite