Apache rewrite规则的配置过程

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!

wKiom1RTU1DSiFOZAAGpXNVVQrg789.jpglinux :
[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!

wKiom1RTU9_TbdNOAAMlTbzyLlw807.jpg


注意:

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书写。




      本文转自独弹古调  51CTO博客,原文链接:http://blog.51cto.com/hunkz/1570459,如需转载请自行联系原作者





  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值