benzhou 发表于 2018-12-21 09:33:29

PHP伪静态化页面的实现

方法一:  一 打开 Apache 的配置文件 httpd.conf 。
  二 将#LoadModule rewrite_module modules/mod_rewrite前面的#去掉
  三 在 httpd.conf中添加:
  
  RewriteEngine On
  #RewriteCond %{ENV:SCRIPT_URL} (?:index|dispbbs)[-0-9]+\.html
  RewriteRule ^(.*?(?:index|dispbbs))-([-0-9]+)\.html 1.php?__is_apache_rewrite=1&__rewrite_arg=2
  四 要实现asp帖子URL到php帖子的映射,在 第三步的和之间添加:
  RewriteMap tolowercase int:tolower
  RewriteCond %{QUERY_STRING} (?:boardid|page|id|replyid|star|skin)\=\d+
  RewriteRule ^(.*(?:index|dispbbs))\.asp 1.php?{tolowercase:%{QUERY_STRING}}&__is_apache_rewrite=1
  五 保存httpd.conf并重启Apache。
  方法二:
  /*
  功能:PHP页面伪静态化
  具体用法:
  实现效果为:test.php/year/2006/action/_add.html
  mod_rewrite();
  $yearn=$_GET["year"];//结果为'2006'
  $action=$_GET["action"];//结果为'_add'
  */
  function mod_rewrite(){
  global $_GET;
  $nav=$_SERVER["REQUEST_URI"];
  $script_name=$_SERVER["SCRIPT_NAME"];
  $nav=substr(ereg_replace("^$script_name","",urldecode($nav)),1);
  $nav=preg_replace("/^.ht(m){1}(l){0,1}$/","",$nav);//这句是去掉尾部的.html或.htm
  $vars = explode("/",$nav);
  for($i=0;$i$_GET["$vars[$i]"]=$vars[$i+1];
  }
  return $_GET;
  }
  ?>
  //利用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
  $sid =intval($arr_path); //取得值100
  $softid =intval($arr_path); //取得值8630
  }else die("Path:Error!");
  //相当于soft.php?gid=1&sid=100&softid=8630
  }else die('Path:Nothing!');
  //就是这么简单了。~)
  ?>
  apache 伪静态设置方法
  一 打开 apache 的配置文件 httpd.conf
  二 将#loadmodule rewrite_module modules/mod_rewrite 前面的#去掉
  三 打开 httpd-vhosts.conf
  四 修改如下:
  
  Options FollowSymLinks
  ServerAdmin kevinliyong@163.com
  DocumentRoot d:\myphp\sophiacn
  ServerName sophiacn.com
  ErrorLog logs/sophiacn.com-error_log
  CustomLog logs/sophiacn.com-access_log common
  php_admin_value open_basedir "d:\myphp\sophiacn;C:\WINDOWS\TEMP"
  RewriteEngine on
  RewriteRule ^(.*)/list-(+)-(+)\.html$ $1/list.php?sort_id=$2&page=$3
  RewriteRule ^(.*)/article-(+)-(+)\.html$ $1/article.php?sort_id=$2&id=$3
  重启 apache
  .htaccess 方法:
  RewriteEngine On
  RewriteBase /
  RewriteRule ^list-(+)-(+)\.html$ list.php?sort_id=$1&page=$2
  RewriteRule ^article-(+)-(+)\.html$ article.php?sort_id=$1&id=$2
  一.Apache设置
  独立主机用户
  Apache 基本配置:
  首先确定您使用的 Apache 版本,及是否加载了 mod_Rewrite 模块。
  Apache 1.x 的用户请检查 conf/httpd.conf 中是否存在如下两段代码:
  LoadModule Rewrite_module libexec/mod_Rewrite.so
  AddModule mod_Rewrite.c
  Apache 2.x 的用户请检查 conf/httpd.conf 中是否存在如下一段代码:
  LoadModule Rewrite_module modules/mod_Rewrite.so
  如果没有安装 mod_Rewrite,您可以重新编译 Apache,并在原有 configure 的内容中加入 --enable-Rewrite=shared
  注:如果前面有#,将其去掉。
  方法一:通过配置Apache配置文件httpd.conf实现URL重写
  在配置文件(通常就是 conf/httpd.conf)中加入如下代码。
  
  RewriteEngine On
  RewriteRule ^(.*)/archiver/((fid|tid)-[\w\-]+\.html)$ $1/archiver/index.php?$2
  RewriteRule ^(.*)/forum-(+)-(+)\.html$ $1/forumdisplay.php?fid=$2&page=$3
  RewriteRule ^(.*)/thread-(+)-(+)-(+)\.html$ $1/viewthread.php?tid=$2&extra=page\%3D$4&page=$3
  RewriteRule ^(.*)/space-(username|uid)-(.+)\.html$ $1/space.php?$2=$3
  RewriteRule ^(.*)/tag-(.+)\.html$ $1/tag.php?name=$2
  注:此时请务必注意,如果网站使用通过虚拟主机来定义,请务必加到虚拟主机配置,即 中去,如果加在虚拟主机配置外部将可能无法使用,改好后将 Apache 重启。
  方法二:通过在根目录中的跨越配置文件.htaccess实现URL重写
  1. 配置apache支持对 .htaccess 文件的解析
  查找:
  
  Options FollowSymLinks
  AllowOverride None
  修改为:
  
  Options FollowSymLinks
  AllowOverride All
  man对AllowOverride 的解释:
  AllowOverride controls what directives may be placed in .htaccess files.
  It can be "All", "None", or any combination of the keywords:
  Options FileInfo AuthConfig Limit
  就是说,将None改为All,.htaccess文件才能被支持!
  2. 创建.htaccess文件Win32 系统下,无法直接建立 .htaccess 文件,您可以从其他系统中拷贝一份,或者在 Discuz.net 技术支持栏目中下载此文件。
  3. 编辑.htaccess文件
  # 将 RewriteEngine 模式打开
  RewriteEngine On
  # 指向 php 文件所在目录如果php文件与 .htaccess 在同一目录则为 RewriteBase /
  RewriteBase /
  # Rewrite 规则
  RewriteRule ^archiver/((fid|tid)-[\w\-]+\.html)$ archiver/index.php?$1
  RewriteRule ^forum-(+)-(+)\.html$ forumdisplay.php?fid=$1&page=$2
  RewriteRule ^thread-(+)-(+)-(+)\.html$ viewthread.php?tid=$1&extra=page\%3D$3&page=$2
  RewriteRule ^space-(username|uid)-(.+)\.html$ space.php?$1=$2
  RewriteRule ^tag-(.+)\.html$ tag.php?name=$1
  租用空间用户
  1. 首先咨询您的空间服务商,空间是否支持 Rewrite 以及是否支持对站点目录中 .htaccess 的文件解析,否则即便按照下面的方法设置好了,也无法使用。
  2. 创建.htaccess文件Win32 系统下,无法直接建立 .htaccess 文件,您可以从其他系统中拷贝一份,或者在 Discuz.net 技术支持栏目中下载此文件。
  3. 编辑.htaccess文件
  # 将 RewriteEngine 模式打开
  RewriteEngine On
  # 指向 php 文件所在目录如果php文件与 .htaccess 在同一目录则为 RewriteBase /
  RewriteBase /
  # Rewrite 规则
  RewriteRule ^archiver/((fid|tid)-[\w\-]+\.html)$ archiver/index.php?$1
  RewriteRule ^forum-(+)-(+)\.html$ forumdisplay.php?fid=$1&page=$2
  RewriteRule ^thread-(+)-(+)-(+)\.html$ viewthread.php?tid=$1&extra=page\%3D$3&page=$2
  RewriteRule ^space-(username|uid)-(.+)\.html$ space.php?$1=$2
  RewriteRule ^tag-(.+)\.html$ tag.php?name=$1
  二.Rewrite 规则
  上面无论是在apache中设置的:
  
  RewriteEngine On
  RewriteRule ^(.*)/archiver/((fid|tid)-[\w\-]+\.html)$ $1/archiver/index.php?$2
  RewriteRule ^(.*)/forum-(+)-(+)\.html$ $1/forumdisplay.php?fid=$2&page=$3
  RewriteRule ^(.*)/thread-(+)-(+)-(+)\.html$ $1/viewthread.php?tid=$2&extra=page\%3D$4&page=$3
  RewriteRule ^(.*)/space-(username|uid)-(.+)\.html$ $1/space.php?$2=$3
  RewriteRule ^(.*)/tag-(.+)\.html$ $1/tag.php?name=$2
  还在是文件.htaccess中添加的:
  # 将 RewriteEngine 模式打开
  RewriteEngine On
  # 指向 php 文件所在目录如果php文件与 .htaccess 在同一目录则为 RewriteBase /
  RewriteBase /
  # Rewrite 规则
  RewriteRule ^archiver/((fid|tid)-[\w\-]+\.html)$ archiver/index.php?$1
  RewriteRule ^forum-(+)-(+)\.html$ forumdisplay.php?fid=$1&page=$2
  RewriteRule ^thread-(+)-(+)-(+)\.html$ viewthread.php?tid=$1&extra=page\%3D$3&page=$2
  RewriteRule ^space-(username|uid)-(.+)\.html$ space.php?$1=$2
  RewriteRule ^tag-(.+)\.html$ tag.php?name=$1
  其中都指明了URL重写规则。
  请看:RewriteRule ^forum-(+)-(+)\.html$ forumdisplay.php?fid=$1&page=$2
  这句说明了URL为forumdisplay.php?fid=$1&page=$2可以写成forum-(+)-(+)\.html这种模式。
  如:访问http://localhost/ forumdisplay.php?fid=1&page=2与访问http://localhost/ forum-1-2.html的效果是一样的!
  注:这些规则是可以自己写正则表达式随意更改的。根据自己需要的格式。来定制URL重写规则。

页: [1]
查看完整版本: PHP伪静态化页面的实现