至于apache的伪静态要作用的启用那些就不讲了。
现在主要讲: 一个虚拟主目中有几个子目录有一些文件,当访问的文件存在时就直接返回文件,否则就返回伪静态的查询结果。
环境: apache2.2.15 + rhel6u4_x64
[root@redhat6 webb]# pwd
/webroot/webb
[root@redhat6 webb]# ls -F
a/ dir.php index.html Redhat3/ Redhat4/ Redhat5/ Redhat6/ Redhat7/
[root@redhat6 webb]# ls Redhat4
a.html d.html g.html j.html m.html p.html s.html v.html y.html
b.html e.html h.html k.html n.html q.html t.html w.html z.html
c.html f.html i.html l.html o.html r.html u.html x.html
dir.php 的作用,只接收一个参数,就显示出来:
[root@redhat6 webb]# cat dir.php <html> <body> <h1> web b dir to <?php echo $_GET[‘id‘]; ?>. </h1> </body> </html>
。。。。
要求: 如果访问到存的存在文件时,就返回文件本身,如果是存在的目录是,就显示目录是的内容,
如果是不存在文件时,就会用伪静态显示出结果:
配置:
<VirtualHost *:9002> DocumentRoot /webroot/webb/ ServerName b.long.com <Directory "/webroot/webb"> Options Indexes order deny,allow allow from all </Directory> RewriteEngine On #启用rewrite RewriteCond %{REQUEST_FILENAME} !-f #判断文件是否存在 RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d #判断真实文件或目录是否存在, %{DOCUMENT_ROOT}是当前的虚拟主机的根目录 [QSA,L] RewriteRule ^/([/w]*)$ %{DOCUMENT_ROOT}/dir.php?id=$1 [QSA,L] #如果以上的文件时,过就行rewrite功能,将([/w]*)转化为后面的请求。 </VirtualHost>
重启服务: service httpd restart
结果显示:
存在的文件
不存在的文件:
本文出自 “海无涯” 博客,请务必保留此出处http://plong.blog.51cto.com/3217127/1400803
apache rewrite 功能之根据是文件是否存在进行伪静态,布布扣,bubuko.com
apache rewrite 功能之根据是文件是否存在进行伪静态
原文:http://plong.blog.51cto.com/3217127/1400803