PHP學習筆記:偽靜態規則的書寫


這里以阿帕奇為服務器軟件,直接上案例:

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

 

5.訪問文件名+html效果等效后綴為php的文件

 

RewriteEngine On Options -Indexes RewriteRule ^/?([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

 6.訪問所有目錄下的文件等效於訪問文件名+后綴

RewriteEngine On Options -Indexes RewriteRule ^/?([a-z/]+)$ $1.php [NC]

效果:  

    訪問www.test.com/index效果等於www.test.com/index.php

    訪問www.test.com/abc/abc效果等於www.test.com/abc/abc.php

7.404頁面跳轉設置

  目標:當用戶訪問我們不存在的頁面時候,把網站內容跳轉到404頁面。在這里,我把404.html作為404頁面,放在網站的根目錄。

 

RewriteEngine On
Options -Indexes
ErrorDocument 404 /404.html

 

  效果:

  各位在制作404頁面可以直接百度上找些模版,再改改,頁面會更加美觀,建議在404頁面設置3秒鍾跳轉首頁、增加設置返回首頁鏈接,這樣對搜索引擎和用戶更加友好。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM