PHP伪静态入门基于教程
Apache伪静态html(URL Rewrite)设置法
phpma一 打开 Apache 的配置文件 httpd.conf 。
phpma二 将#LoadModule rewrite_module modules/mod_rewrite前面的#去掉
1、apache
现在我们可以实现伪静态页面了,写下一下的规则:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4274')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4274><ifmodule mod_rewrite.c> #liunx下需要写
rewriteengine on
rewriterule ([a-za-z]{1,})-([0-9]{1,}).html$ index.php?action=$1&id=$2
</ifmodule>
([a-za-z]{1,})-([0-9]{1,}).html$是规则,index.php?action=$1&id=$2是要替换的格式,$1代表第一个括号匹配的值,$2代表第二个
然后重启动apache
2、.htaccess 在目录里新建个文件命名为 .htaccess
、.htaccess文件的书写
在网站根目录下建立一个文件,名称是.htaccess,书写方式:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2873')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2873>RewriteEngine on #开启重写
RewriteRule ^/$ index.php #表示用“/”就可以访问index.php
RewriteRule ^about_(d*)/$ about/about.php?id=$1 #表示可以使用about_22/访问到about/about.php?id=22页面。注意$前面的“/”
RewriteRule ^about_(d*).html$ about/about.php?id=$1 #表示可以使用about_22.html访问到about/about.php?id=22页面RewriteRule ^news_(d*)_(d*).html$ news/news.php?id=$1&page=$2 #表示可以使用news_11_2.html访问到news/news.php?id=11&page=2页面。$1表示第一个参数,$2表示第二个参数
从上面可以看出,如果我们有下面的链接
<a href="/about_22/">关于我们</a>
那么我们访问到的页面与使用下面访问的页面一样
<a href="/about/about.php?id=22">关于我们</a>
2.php代码
比如:http://www.xxxx.com/soft.php/1,100,8630.html
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4251')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4251>< ?php
//利用server变量 取得PATH_INFO信息 该例中为 /1,100,8630.html 也就是执行脚本名后面的部分
if(@$path_info =$_SERVER["PATH_INFO"]){
//正则匹配一下参数
if(preg_match("//(d ),(d ),(d ).html/si",$path_info,$arr_path)){
$gid =intval($arr_path[1]); //取得值 1
$sid =intval($arr_path[2]); //取得值100
$softid =intval($arr_path[3]); //取得值8630
}else die("Path:Error!");
//相当于soft.php?gid=1&sid=100&softid=8630
}else die('Path:Nothing!');
//就是这么简单了。~)
?>
您可能感兴趣的文章:
PHP实现页面静态化、纯静态化及伪静态化
适合入门的php基础系列教程
php伪静态如何获取地址栏url
PHP伪静态入门基于教程
apache伪静态转nginx
php入门教程(索引)
windows与linux伪静态规则写法
php中url伪静态和Gzip同时开启引发冲突的原因与解决方法
php环境中url伪静态和Gzip同时开启相关问题与解决办法
apache伪静态怎么配置?