php Apache配置伪静态的介绍

以下是摘抄http://jingyan.baidu.com/article/86112f132aa7462737978718.html的,作为记录,方便以后参考

现有的在线网上视频教程对伪静态的讲解比较简单,但不全面,我以一个真实案例来讲解伪静态的制作过程。

步骤开始:

(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

上面的url重写规则是我的一个真实案例,详见http://www.xlyun.com对比参考

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>';

}

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

原文地址:https://www.cnblogs.com/zonglonglong/p/5010209.html