这里以阿帕奇为服务器软件,直接上案例:
1.把index.html重定向到index.php
<span style="font-size: 18px;"><span style="color: #000000;">RewriteEngine On Options </span>-<span style="color: #000000;">Indexes </span><span style="color: #000000;"> ReWriteRule </span>^index.html$ /index.php [NC]</span>
效果:访问www.test.com/index.html效果等于www.test.com/index.php
2.把index重定向到index.php
<span style="font-size: 18px;"><span style="color: #000000;">RewriteEngine On Options </span>-<span style="color: #000000;">Indexes </span><span style="color: #000000;"> ReWriteRule </span>^index$ /index.php [NC]</span>
效果:访问www.test.com/index效果等于www.test.com/index.php
3.根目录下的PHP文件,用html后缀也能访问
写法1:
<span style="font-size: 18px;"><span style="color: #000000;">RewriteEngine On Options </span>-<span style="color: #000000;">Indexes ReWriteRule </span>^([a-zA-Z0-9_]{1,})\.html$ /$1.php [NC]</span>
效果: 访问www.test.com/index.html效果等于www.test.com/index.php
访问www.test.com/abc.html效果等于www.test.com/abc.php
4.根目录下的php文件,直接访问文件名效果一样
<span style="font-size: 18px;"><span style="color: #000000;">RewriteEngine On Options </span>-<span style="color: #000000;">Indexes ReWriteRule </span>^([a-zA-Z0-9_]{1,})$ /$1.php [NC]</span>
效果: 访问www.test.com/index效果等于www.test.com/index.php
访问www.test.com/abc效果等于www.test.com/abc.php
5.访问文件名+html效果等效后缀为php的文件
<span style="color: #000000;">RewriteEngine On Options </span>-<span style="color: #000000;">Indexes RewriteRule </span>^/?([a-z/]+)\.html$ $1.php [NC]
效果:
访问www.test.com/index.html效果等于www.test.com/index.php
访问www.test.com/abc/abc.html效果等于www.test.com/abc/abc.php