PHP的mod_rewrite重寫模組將.php尾碼換成.html

來源:互聯網
上載者:User

標籤:mod_rewrite   php mod_rewrite   

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 onRewriteRule ^/?([a-zA-Z0-9]+)\.html$ $1.php [L]



建立無檔案尾碼名連結
如果你想使你的PHP網站的連結更加簡潔易記-或者隱藏檔案的尾碼名,試試這個:
RewriteRule ^/?([a-z]+)$ $1.php [L]



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.