php语言 百分网手机站

php的apache伪静态

时间:2020-09-04 14:21:14 php语言 我要投稿

php的apache伪静态

  导语:现有的在线网上视频教程对伪静态的讲解比较简单,但不全面,小编以一个真实案例来讲解伪静态的制作过程。欢迎参考!

  步骤开始:

  (1) 启用rewrite模块,在默认情况下,没有启用

  修改httpd.conf文件,启动rewrite模块

  去掉LoadModule rewrite_module modules/mod_rewrite.so前的#号即可

  (2) 配置我们的虚拟主机

  httpd.conf 打开虚拟主机的配置文件

  # Virtual hosts

  Include conf/extra/httpd-vhosts.conf

  修改 httpd-vhost.conf

  <VirtualHost *:80>

  DocumentRoot "F:/Appserv/www/xh"

  ServerName xh.com

  <Directory "F:/Appserv/www/xh">

  AllowOverride All

  </Directory>

  </VirtualHost>

  我是用的是appserv集成环境,安装在F盘

  (3) 在hosts文件中,配置ip和主机的对应关系

  127.0.0.1 xh.com

  (4) 在F:/Appserv/www/xh目录下建立.htaccess文件,写入

  <IfModule mod_rewrite.c>

  RewriteEngine on

  RewriteRule ^([0-9]+).html$   index.php/Index/index/p/$1

  RewriteRule ^([A-Z])_(d+).html$   index.php/List/index/first_letter/$1/p/$2

  RewriteRule ^([A-Z]).html$   index.php/List/index/first_letter/$1

  </IfModule>

  解释一下上面那段话,

  访问2.html  =>  index.php/Index/index/p/2

  D_2.html  =>  index.php/List/index/first_letter/D/p/2

  D.html  =>  index.php/List/index/first_letter/D

  2.html表示全部歇后语的第二页,D_2.html表示以字母D打头的歇后语的第二页,而单独一个字母D就表示以D打头的以第一页

  好了问题来了,大部分教程只告诉你怎么在.htaccess中重写url,那么我们要让用户点击时显示的也是静态网址,这样表意清晰,目录结构简单,对用户对搜索引擎都比较友好,我们是不会在地址栏里头一个一个的敲入静态网址的,这个问题该怎么解决呢?

  很简单,只需对模板中的分页标签变量{$page}做一个简单的正则替换,如下,

  首页列表分页的替换:

  <div class="pagination"><?php echo preg_replace('/index.php/Index/index/p/(d+).html/','$1.html',$page); ?></div>

  字母列表分页的`替换:<div class="pagination"><?php echo preg_replace('/index.php/List/index/first_letter/([A-Z])/p/(d+).html/','$1_$2.html',$page); ?></div>

  循环26个字母的改写(去掉没有结果的那些字母,只需做一个简单的链接改写,改成 字母.html 即可,无需正则替换)

  for($i=97;$i<=122;$i++) {

  $c = strtoupper(chr($i));

  if($c==I || $c==U || $c==V) continue;

  echo '<li><a href="' . $c . '.html">'.$c.'</a></li>';

  }

  好了,伪静态就这么简单,我以这个简单的例子阐述了伪静态从头到尾的过程,方便大家学习和交流,目的在于针对多数教程的一个补充,需要完成更复杂任务的同学,请自行深入研究伪静态吧!


【php的apache伪静态】相关文章:

PHP伪静态的方法09-28

apache服务器伪静态教程08-25

php简单伪静态实例09-28

PHP伪静态的几种方法09-10

PHP运行于Apache 模块方式06-08

Java UrlRewriter伪静态技术运用分析12-01

Linux+Apache+Mysql+PHP优化技巧09-20

生成PHP网站页面静态化的方法09-17

php技术生成静态页面的方法08-15