我正试图在products.php
页面上获得$_GET['pid']
的值,看起来它不适合我,任何帮助都将不胜感激。
.htaccess
重写规则如下
RewriteEngine On
RewriteRule ^products/([0-9]+)$ /products.php?pid=$1 [NC,L]
index . php
<?php
$i=1;
while($i <= 10){
echo '<a href="products.php?pid='.$i.'">Products '.$i.'</a><br />';
$i++;
}
?>
products.php
<?php
if(!isset($_GET['pid'])){
header('location : index.php');
}
else if(isset($_GET['pid'])){
echo 'Product id - '.$_GET['pid'];
}
?>
你的意思是链接没有正确显示?您的php文件必须输出链接,稍后mod_rewrite将其"翻译"。请看下面的例子:
<?php
$i=1;
while($i <= 10){
echo '<a href="products/'.$i.'">Products '.$i.'</a><br />';
$i++;
}
?>
我们使用Htaccess将"丑陋"的URL重写得更短更漂亮。在你的代码中,你改变了你的htaccess.
RewriteEngine On
RewriteRule ^(.*)$ products.php?pid=$1 [QSA,L]
然后修改你的URL。
<?php
$i=1;
while($i <= 10){
echo '<a href="products.php/'.$i.'">Products '.$i.'</a><br />';
$i++;
}
?>