教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php 伪静态 url重写简单示例

php 伪静态 url重写简单示例

发布时间:2016-01-27   编辑:jiaochengji.com
本文介绍php下实现伪静态,即url重写的简单例子,有需要的朋友,可以参考下。

1、新建一个记事本文件,重命名为 .htaccess ,放到根目录中。
内容如下:
 

复制代码 代码示例:
RewriteEngine on
RewriteRule ^article/index.html$ article/index.php
RewriteRule ^article/detail-(.[0-9]*).html$ article/index.php?type=detail&id=$1
RewriteRule ^article/list-(.[0-9]*)-(.[0-9]*).html$ article/index.php?type=list&classid=$1&offset=$2
RewriteRule ^product/index.html$ product/index.php
RewriteRule ^product/detail-(.[0-9]*).html$ product/index.php?type=detail&id=$1
RewriteRule ^product/list-(.[0-9]*)-(.[0-9]*).html$ product/index.php?type=list&classid=$1&offset=$2
RewriteRule ^index.html$ index.php
RewriteRule ^aboutus/detail-(.[0-9]*).html$ aboutus/index.php?type=detail&id=$1
RewriteRule ^aboutus/detail-(.[0-9]*)-(.[0-9]*).html$ aboutus/index.php?type=list&classid=$1&offset=$2

说明:
aboutus、article、product是模块名。
 

复制代码 代码示例:
RewriteRule ^aboutus/detail-(.[0-9]*)-(.[0-9]*).html$ aboutus/index.php?type=list&classid=$1&offset=$2
 

这个是aboutus模块分页的。
RewriteRule ^index.html$ index.php 是首页伪静态重写
如果有哪个模块需要伪静态,都要参考上面的写进 .htaccess 文件里面。

2、到网站后台的系统管理——配置管理——需要伪静态的模块链接模式改为2即可,其他的不要改。

3、对上面地址重写后,需要修改链接地址,比如导航的链接地址,拿aboutus模块的第一个id的链接地址来讲怎么写成静态地址,上面的 aboutus/detail-(.[0-9]*).html ,这里的(.[0-9]*) 就改成1,修改成静态地址是这样 aboutus/detail-1.html 也可以自定义添加1个链接模式就可以了。

4、删除缓存,比如article 模块,首页的地址还是article目录,不用改,打开article模块,然后看到文章的链接都是静态的了。
注意:如果把.htaccess,放到根目录下出现网页打不开等错误,删除 .htaccess  网站可以正常打开,原因可能是环境不支持。

您可能感兴趣的文章:
php 伪静态 url重写简单示例
php 伪静态(url重写)的写法
php实现伪静态的二种方法介绍
php url伪静态过程详解
php环境中url伪静态和Gzip同时开启相关问题与解决办法
php中url伪静态和Gzip同时开启引发冲突的原因与解决方法
伪静态几种做法
php runtime、http web中rewrite浅解和方案
PHP伪静态页面函数及使用方法介绍
C# .NET自定义类实现伪静态页面的代码

[关闭]
~ ~