要根据数据库中已有的数据,例如产品名称或种类名称,使用函数来生成新的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)
{
//删除除了字母、数字、连接符、空格、下划线之外的字符,用空格代替
$NOT_acceptable_characters_regex='#[^-a-zA-Z0-9]#';//#是php中加入正则表达式
$string=preg_replace($NOT_acceptable_characters_regex,'',$string);
$string=trim($string);//清除从参数中接收的字符串中的前后空格
$string=preg_replace('#[-_ ]#','-',$string);//将空格、破折号和下划线转换为破折号
return $string;
}
function make_category_product_url($category_name,$category_id,$product_name,$product_id)
{
$clean_category_name=_prepare_url_text($category_name);
$clean_product_name=_prepare_url_text($product_name);
//连接字符串来生成链接字符串
$url=SITE_DOMAIN . '/Products/' .
$clean_category_name . '-C' . $category_id . '/' .
$clean_product_name . '-P' . $product_id . '.html';
return $url;
}
?>
2.创建catalog.php,内容:
<?php
require_once("url_factory.inc.php");
?>
<html>
<head>
<title>wskk8</title>
</head>
<body>
<h1>Products on Promotion at SEO Egghead Shop</h1>
<ul>
<li>
<a href="<?php echo make_category_product_url("Carpenter Tools", 12, "Belt Sander", 45); ?>">
lulu test1:Belt Sander
</a>
</li>
<li>
<a href="<?php echo make_category_product_url("Friends Shed", 2, "AJAX PHP Book", 42); ?>">
lulu test2:AJAX PHP Book
</a>
</li>
</ul>
</body>
</html>
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,内容:
<?php
define ('SITE_DOMAIN','http://localhost/testphp');
?>
6.用http://localhost/include/catalog.html访问
结果:
创建两个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)
{
//删除除了字母、数字、连接符、空格、下划线之外的字符,用空格代替
$NOT_acceptable_characters_regex='#[^-a-zA-Z0-9]#';//#是php中加入正则表达式
$string=preg_replace($NOT_acceptable_characters_regex,'',$string);
$string=trim($string);//清除从参数中接收的字符串中的前后空格
$string=preg_replace('#[-_ ]#','-',$string);//将空格、破折号和下划线转换为破折号
return $string;
}
function make_category_product_url($category_name,$category_id,$product_name,$product_id)
{
$clean_category_name=_prepare_url_text($category_name);
$clean_product_name=_prepare_url_text($product_name);
//连接字符串来生成链接字符串
$url=SITE_DOMAIN . '/Products/' .
$clean_category_name . '-C' . $category_id . '/' .
$clean_product_name . '-P' . $product_id . '.html';
return $url;
}
?>
2.创建catalog.php,内容:
<?php
require_once("url_factory.inc.php");
?>
<html>
<head>
<title>wskk8</title>
</head>
<body>
<h1>Products on Promotion at SEO Egghead Shop</h1>
<ul>
<li>
<a href="<?php echo make_category_product_url("Carpenter Tools", 12, "Belt Sander", 45); ?>">
lulu test1:Belt Sander
</a>
</li>
<li>
<a href="<?php echo make_category_product_url("Friends Shed", 2, "AJAX PHP Book", 42); ?>">
lulu test2:AJAX PHP Book
</a>
</li>
</ul>
</body>
</html>
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,内容:
<?php
define ('SITE_DOMAIN','http://localhost/testphp');
?>
6.用http://localhost/include/catalog.html访问
结果:
点击链接后效果: