要根據資料庫中已有的資料,例如產品名稱或種類名稱,使用函式來生成新的url位址,實現一致性
建立兩個php函式
1. _prepare_url_text接收乙個字串引數(比如產品或種類名稱,它將被用於包含到被重寫後的url位址中),
並將它轉換為能夠放入url位址中的相應格式,比如:函式可以將「super drill」轉換為"super-drill"
2. make_product_url接收產品和種類的名稱及id作為引數,並使用_prepare_url_text函式來生成
乙個類似「/products/tools-c5/super-drill/p9.html」的url位址
步驟:1.建立include資料夾,並新增config.inc.php檔案,內容如下
<?php
require_once 'config.inc.php';
function _prepare_url_text($string)
function make_category_product_url($category_name,$category_id,$product_name,$product_id)
?>
2.建立catalog.php,內容:
<?php
require_once("url_factory.inc.php");
?>
3.建立product.php,內容:
<?php
echo $_get['product_id'] . 'and ' . $_get['category_id'];
?>
4.建立.htaccess檔案,內容:
rewriteengine on
#rewriterule ^mytest\.html$ /testphp/product.php?product_id=123
rewriterule ^products/c([0-9]+)/p([0-9]+)\.html$ /include/product.php?category_id=$1&product_id=$2 [l]
rewriterule ^products/.*-c([0-9]+)/.*-p([0-9]+)\.html$ /include/product.php?category_id=$1&product_id=$2 [l]
rewriterule ^catalog.html$ /include/catalog.php [l]
5.config.inc.php,內容:
6.用http://localhost/include/catalog.html訪問
結果:
慎用url重寫
為了使 url位址更加友好 當然可能有別的原因 很多站點使用了 url重寫,如http www.cnblogs.com life,在asp.net中通常要處理這樣的 url重寫,必須在iis中將 對映到aspnet isapi.dll c windows microsoft.net framewor...
url重寫技術
注釋掉 條目,以使用 http 模組執行重寫 注釋掉 條目,以使用 http 處理程式執行重寫。除了指定使用 http 模組還是 http 處理程式執行重寫外,web.config 檔案還包含重寫規則 重寫規則由兩個字串組成 要在被請求的 url 中查詢的模式 要替換此模式的字串 如果找到 在 we...
慎用url重寫
為了使url位址更加友好 當然可能有別的原因 很多站點使用了url重寫,如 http www.cnblogs.com life,在asp.net中通常要處理這樣的url重寫,必須在iis中將 對映到aspnet isapi.dll c windows microsoft.net framework ...