利用.htaccess实现伪静态方法

首先配置服务器启动重写模块
打开 Apache 的配置文件 httpd.conf 。
将#LoadModule rewrite_module modules/mod_rewrite前面的#去掉。
保存后重启Apache

 

例如:接受URL里的id参数,并在页面显示。
test.php代码如下:

<?php
$id=$_GET[“id”];
echo $id;
?>

新建一个文档,并保存为.htaccess,在里面写入如下代码:

<IFMODULE mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^t_(.*).html$ test.php?id=$1 [L]
</IFMODULE>

第3行为网站目录:(如test/);

第4行 ^t_(.*).html$ 描述你输入的URL地址, test.php?id=$1  其实际要访问的地址

          最好写成(RewriteRule ^g_([0-9]+[a-zA-Z]+)_([0-9]+[a-zA-Z]+)_([0-9]+[a-zA-Z]+)\.html$ test\.php\?id=$1&page=$2)

          第一个()为$1,第二个()为$2;[0-9]为数字,[a-zA-Z]为字母
例如当您在浏览器中输入(假设您的test.php和.htaccess文件都在您服务器的mytest文件夹下)localhost/mytest/t_1.html 则在浏览器中将输出 1, 
如果输入为 localhost/mytest/t_sophp.html 则浏览器输出sophp
第一个地址实际访问的是localhost/mytest/test.php?id=1
第二个实际访问的是 localhost/mytest/test.php?id=sophp

重写完规则后,将您网页中以前都为传参数的URL链接改为您修改后的规则样式就成功了。
如果自己买的空间支持重写的话,只要把.htaccess 文件传到根目录即可。

 

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注