這裡以阿帕奇為服務器軟件,直接上案例:
1.把index.html重定向到index.php
RewriteEngine On Options -Indexes ReWriteRule ^index.html$ /index.php [NC]
效果:訪問www.test.com/index.html效果等於www.test.com/index.php
2.把index重定向到index.php
RewriteEngine On Options -Indexes ReWriteRule ^index$ /index.php [NC]
效果:訪問www.test.com/index效果等於www.test.com/index.php
3.根目錄下的PHP文件,用html後綴也能訪問
寫法1:
RewriteEngine On Options -Indexes ReWriteRule ^([a-zA-Z0-9_]{1,})\.html$ /$1.php [NC]
效果: 訪問www.test.com/index.html效果等於www.test.com/index.php
訪問www.test.com/abc.html效果等於www.test.com/abc.php
4.根目錄下的php文件,直接訪問文件名效果一樣
RewriteEngine On Options -Indexes ReWriteRule ^([a-zA-Z0-9_]{1,})$ /$1.php [NC]
效果: 訪問www.test.com/index效果等於www.test.com/index.php
訪問www.test.com/abc效果等於www.test.com/abc.php