Apache rewrite规则的配置过程
Apache rewrite规则的配置过程服务器端:192.168.10.201 (RHEL 5.9)
客户端:192.168.10.40(Linux)
192.168.10.246 (windows xp)
1.安装Apache
# yum -y install httpd
# ls /etc/httpd/modules/mod_rewrite.so
mod_rewrite.so##检查确定有此模块
vim /etc/httpd/conf/httpd.conf
LoadModule rewrite_module modules/mod_rewrite.so ##检查确定有此行
2.创建基于域名的虚拟主机
# vim /etc/httpd/conf.d/vhosts.conf
ServerAdmin hunk.test.com
DocumentRoot"/var/www/html/hunk"
ServerName192.168.10.201
RewriteEngine on ##打开rewirte功能
Include conf.d/hunk-rewrite.conf
3.写rewrite规则
# vim /etc/httpd/conf.d/hunk-rewrite.conf
RewriteCond %{HTTP_HOST} hunk.test.com
RewriteRule^(.*)/index.html$http://hunk.test.com/test.html
4.在/var/www/html/hunk 添加测试页面。
# vim /var/www/html/hunk/index.html
test page
this is test page !page jump failure!
# vim /var/www/html/hunk/hunk.html
test page
this is test page !page jump success!
客户端设置
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 :
# vim /etc/hosts
192.168.10.40 hunk.test.com
# 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 中,^表示以某某开头,$表示以某某结尾, .(点)表示匹配任意单个字符,*表示匹配前面的子表达式零次或多次(大于等于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书写。
页:
[1]