2.在http.conf里面查找Options FollowSymLinks,将后面的AllowOverride None改成AllowOverride All。 这个时候兵分两路,有http.conf权限的可以自己在http.conf后面添加url rewrite规则 如 <IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)/([0-9]{1,}).html$ $1/page.php?id=$2 </IfModule> 第二路就只好在代码根目录建立.htaccess文件,这个文件建立比较简单,有3种方法,最简单的当然是建立1.htaccess然后记事本打开另存为.htaccess,另外就是cmd下rename和ftp软件修改。 3.rewrite规则 看个例子就懂了 <IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)/([0-9]{1,}).html$ $1/page.php?id=$2 </IfModule> $是终止符,前面写个正则表达式,$1获取第一个子模式(小括号里面的东东),$2获取第二个子模式东东。 友情提示 ^是开头,不要忘记加了 如果有2个参数,也很简单,自行处理。 也就是说浏览器里面显示的如果是4.html(你的文件里面的链接写成.html的)的话,实际调用的是page.php?id=4,这样浏览器的输入栏都是html,达到了伪静态。 当然你改写不同正则,显示的url当然就不同了,随你喜欢的该。 遇见多行的话不要妄自加分号。如下才是对的: <IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)/([0-9]{1,}).html$ $1/page.php?id=$2 RewriteRule ^(.*)/index.html$ $1/index.php RewriteRule ^(.*)/wenzhang.html$ $1/wenzhang.php RewriteRule ^(.*)/gushi.html$ $1/gushi.php RewriteRule ^(.*)/sanwen.html$ $1/sanwen.php RewriteRule ^(.*)/zawen.html$ $1/zawen.php RewriteRule ^(.*)/shige.html$ $1/shige.php RewriteRule ^(.*)/riji.html$ $1/riji.php RewriteRule ^(.*)/xiaoshuo.html$ $1/xiaoshuo.php RewriteRule ^(.*)/contact.html$ $1/contact.php </IfModule>
综述就是网址重写引擎和指定一些重写规则 .,尝试Faking URL Suffix(伪造URL后缀),看起来很静态化,提高了安全性和SEO友好度。 4.重启apache。 windows的httpd.conf很好找,在conf目录(我指的是你如果没用集成包的话) Linux在opt/lampp/etc目录里面有httpd.conf(一般linux下推荐用Xampp按照包,安装在opt目录) 同样进行上面工作restart一下就ok login as: root Server refused our key root@XXXXX.com's password: Last login: Thu Jul 21 14:26:26 2011 from 112.65.219.75 [root@XXXXX ~]# who root pts/0 2011-07-21 14:32 (112.65.219.75) [root@XXXXX ~]# /opt/lampp/lampp restart Stopping XAMPP for Linux 1.6.4... XAMPP: Stopping Apache with SSL... XAMPP: Stopping MySQL... XAMPP: Stopping ProFTPD... XAMPP stopped. Starting XAMPP for Linux 1.6.4... XAMPP: Starting Apache with SSL (and PHP5)... XAMPP: Starting MySQL... XAMPP: Starting ProFTPD... XAMPP for Linux started. 稍等就行了。 5.部分代码指导: <?php echo "<tr>"; $i=$row['id'];//数据库表insun4中的id列,为了。。。。 echo "<td> <a href=\"$i.html\">".$row['title']."</a></td>"; //echo "<td> <a href=\"page.php?id=$i\">".$row['title']."</a></td>"; //echo "<td> <a href=\"page.php?id=$i\">".$row['description']."</a></td>"; echo "<td> <a href=\"$i.html\">".$row['description']."</a></td>"; echo "</tr>"; ?>
<?php include 'config.php'; set_time_limit(0);//防止超时显示错误。 mysql_query("set names'gbk'");//utf8 //$url = "http://www./"; $gid = $_GET['id']; $query="select * from insun4 where id = $gid" ;//用like选择性输出。where link like'http://www./yuanchuang/%' //echo $query; $queryresult=mysql_query($query) or die("查询数据失败");//执行查询 //echo var_dump($queryresult);
while($row = mysql_fetch_array($queryresult)){ echo ($row['content'])."<br>"; } ?>
<IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)/([0-9]{1,}).html$ $1/page.php?id=$2 RewriteRule ^(.*)/index.html$ $1/index.php RewriteRule ^(.*)/wenzhang.html$ $1/wenzhang.php RewriteRule ^(.*)/gushi.html$ $1/gushi.php RewriteRule ^(.*)/sanwen.html$ $1/sanwen.php RewriteRule ^(.*)/zawen.html$ $1/zawen.php RewriteRule ^(.*)/shige.html$ $1/shige.php RewriteRule ^(.*)/riji.html$ $1/riji.php RewriteRule ^(.*)/xiaoshuo.html$ $1/xiaoshuo.php RewriteRule ^(.*)/contact.html$ $1/contact.php </IfModule>
二,看到网上有2-3个方法,都是写个Url_rewrite函数,然后require的,感觉不好就不推荐大家了,大家可以去搜索下。 一般都是借助于apache,nginx,htaccess等,很少用php来实现url的静态化,用php来实现静态化比较麻烦。
如果出现下面这样,说明你没配置好。 如果出现下面这样,说明你配置好了,只是rewrite规则写的不对,检查下好了。 如果出现下面,恭喜你。
|