mod_rewrite是apache的一个功能模块,基于lamp架构的服务器伪静态大多用mod_rewrite来实现网站URL的伪静态。
写好Rewrite伪静态规则,文件保存为.htaccess并存放于网站目录即可。

以下是关于mod_rewrite的一些知识简介。

首先要开启apache的mod_rewrite功能:
在apache目录的Apache\conf这个文件夹下的httpd.conf文件用记事本打开编辑,找到如下一句:
#LoadModule rewrite_module modules/mod_rewrite.so
将前面的#号去掉,变成:
LoadModule rewrite_module modules/mod_rewrite.so
然后再在同一文件找到下面一句:
AllowOverride None
将其改为:
AllowOverride All
然后关闭并保存文件。
这样一来,Apache的mod_rewrite的功能就被开启,非常简单。

下面,我要用mod_rewrite实现这样的URL:
http://xxx.com/0725.html
http://xxx.com/0726.html
http://xxx.com/0727.html
http://xxx.com/****.html
上面链接的意思,就是显示07月25日的历史上发生了什么事情,类似这样。
这样看起来就很美观、整齐了,貌似生成html了。然而它们地址实际是:
http://xxx.com/index.php?today=0725
http://xxx.com/index.php?today=0726
http://xxx.com/index.php?today=0727
http://xxx.com/index.php?today=****

现在我就是要实现把 index.php?today=****变为静态的****.html。以下是代码:
在.htaccess 文件里设置如下规则:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond   %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+).html$   /index.php?today=$1

粗体字这里我说明一下,其它的按这个格式吧,具体我现在也不理解。
[0-9]的意思是,参数只能是0~9这些数字,如果你要包含任何字符,就改为:
RewriteRule ^(.+).html$ /index.php?today=$1
这里[0-9]改为了. ,这个.就代表任意字符。