PHP的mod_rewrite重写模块将.php后缀换成.html

apache Rewrite


mod_rewrite的魔力
简单举例:创建三个文件。分别命名为 test.html,test.php和.htaccess
test.html 输入:
<h1>This is the HTML file.</h1>
test.php输入:
<h1>This is the php file.</h1>
.htaccess输入:
RewriteEngine on
RewriteRule ^/?test\.html$ test.php [L]

将以上三个文件放test測试目录下,在浏览器录入:
http://www.example.com/test/test.html
在浏览器中将 www.example.com替换成你自己的域名。

假设执行结果显示“This is the PHP file”,那么执行成功。假设结果显示“This is the Html file”,那么肯定是哪里出了问题,请你再细致检查下。
假设你測试成功。你是否发现了我们录入了test.html的文件名称,确运行了test.php文件,是的,你已经初识了mod_rewrite的奇妙。


在想要将.php后缀换成.html的文件夹下创建并命名为.htaccess文件。

输入下一段,

RewriteEngine on

RewriteRule ^/?

([a-zA-Z0-9]+)\.html$ $1.php [L]




创建无文件后缀名链接
假设你想使你的PHP站点的链接更加简洁易记-或者隐藏文件的后缀名,试试这个:
RewriteRule ^/?([a-z]+)$ $1.php [L]



posted on 2016-02-29 16:17  gcczhongduan  阅读(651)  评论(0编辑  收藏  举报