您当前的位置: 首页 > 学无止境 > 网站建设 网站首页网站建设
php伪静态详解
发布时间:2015-10-27 14:50:37编辑:雪饮阅读()
php伪静态实质上仅仅只是url请求的重写:
伪静态需要以下步骤来实现:
1、配置rewrite:
搜索关键字“rewrite”于apache配置文件“httpd.conf”中查找到如下:
将其“#”注释去掉保存并重启apache
然后用phpinfo来查看是否支持rewrite:
如图所示在loaded modules选项卡中存在mod_rewrite则是支持的
在配置rewrite中时若是老版本的apache那么其配置文件httpd.conf中或许没有rewrite模块,可以尝试自己手动添加
2、配置虚拟主机:
在hosts文件中解析域名如下:
3、启用虚拟主机:
继续在apache配置文件httpd.conf中搜索关键字“hosts”找到如下:
将其注释去掉并重启apache
4、httpd-vhosts文件配置:
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host2.example.com
#文档根目录
DocumentRoot "c:/Apache2/docs/static"
#域名
ServerName www.hsp.com
ErrorLog "logs/dummy-host2.example.com-error.log"
CustomLog "logs/dummy-host2.example.com-access.log" common
#配置rewrite
<Directory "c:/Apache2/docs/static">
#拒绝所有访问
Deny from all
#是否显示列表(在发布项目后一般不启用)
Options+indexes
#是否启用rewrite
Allowoverride all
</Directory>
</VirtualHost>
虚拟主机启用rewrite后需要在主机根目录新建 “.htaccess”(用notepad++建立,否则直接建立会因为仅有后缀而无法保存)文件并写rewrite规则如下:
#写你的rewrite规则
RewriteEngine On
#news-id(\d+)\.html$是规则 news.php?php=$1是转发页面
RewriteRule news-id(\d+)\.html$ news.php?id=$1
以上的虚拟主机配置在apache2.2.25上面无效,经探索发现apache2.2.25上面的虚拟主机配置rewrite如下所示:
<VirtualHost *:80>
ServerAdmin webmaster@hsp.com
#DocumentRoot "C:\wamp\bin\apache\Apache2.2.21\htdocs\static3"
DocumentRoot "C:\static3"
ServerName www.hsp.com
ErrorLog "logs/dummy-host2.example.com-error.log"
CustomLog "logs/dummy-host2.example.com-access.log" common
#是否启用rewrite
<Directory />
Options FollowSymlinks
AllowOverride All
Order deny,allow
allow from all
</Directory>
</VirtualHost>
传言说write重写还可以以判断(判断是否开启rewrite,若开启了才配置)的形式来配置:
<IfModule rewrite_module>
Options FollowSymlinks
AllowOverride All
Order deny,allow
allow from all
</IfModule>
也可以将重写规则和虚拟主机配置写在一块儿:
<VirtualHost *:80>
ServerAdmin webmaster@hsp.com
#DocumentRoot "C:\wamp\bin\apache\Apache2.2.21\htdocs\static3"
DocumentRoot "C:\static3"
ServerName www.hsp.com
ErrorLog "logs/dummy-host2.example.com-error.log"
CustomLog "logs/dummy-host2.example.com-access.log" common
#是否启用rewrite
<Directory />
Options FollowSymlinks
AllowOverride All
Order deny,allow
allow from all
RewriteEngine On
RewriteRule news-id(\d+)\.html$ news.php?id=$1
</Directory>
</VirtualHost>
此时文件htaccess则已经是无效的了
经过一番探索,发现确实可以在htaccess中判断rewrite是否开启并在开启情况下做出重写规则.如下:
<IfModule rewrite_module>
#写你的rewrite规则
RewriteEngine On
#news-id(\d+)\.html$是规则 news.php?php=$1是转发页面
RewriteRule news-id(\d+)\.html$ /news.php?id=$1
RewriteRule abc-id(\d+)\.html$ error.php?id=$1
</IfModule>
重写规则还可以用在多个目录,每个目录可以拥有不同的规则,再一个站点下新增一个目录public的配置于vhosts中:
<VirtualHost *:80>
ServerAdmin webmaster@hsp.com
#DocumentRoot "C:\wamp\bin\apache\Apache2.2.21\htdocs\static3"
DocumentRoot "C:\static3"
ServerName www.hsp.com
ErrorLog "logs/dummy-host2.example.com-error.log"
CustomLog "logs/dummy-host2.example.com-access.log" common
#是否启用rewrite
<Directory "C:\static3">
Options FollowSymlinks
AllowOverride All
Order deny,allow
allow from all
#RewriteEngine On
#RewriteRule news-id(\d+)\.html$ news.php?id=$1
</Directory>
<Directory "C:\static3\public">
Options FollowSymlinks
AllowOverride All
Order deny,allow
allow from all
#RewriteEngine On
#RewriteRule news-id(\d+)\.html$ news.php?id=$1
</Directory>
</VirtualHost>
然后需要在public目录中建立一个htaccess文件并写规则如下:
<IfModule rewrite_module>
#写你的rewrite规则
RewriteEngine On
#news-id(\d+)\.html$是规则 news.php?php=$1是转发页面
RewriteRule news-id(\d+)\.html$ /news.php?id=$1
RewriteRule abc-id(\d+)\.html$ error.php?id=$1
</IfModule>
然后访问子目录也可以生效了:
关键字词:php,伪静态,个人博客
下一篇:静态化与伪静态案例笔记