I am trying to get the value of $_GET['pid']
on the products.php
page, looks like it's not working for me, any help would be appreciated.
.htaccess
rewrite rule as follows
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'];
}
?>
You mean the links are not shown correctly? Your php file has to output the links, which mod_rewrite "translates" later. See example below:
<?php
$i=1;
while($i <= 10){
echo '<a href="products/'.$i.'">Products '.$i.'</a><br />';
$i++;
}
?>
We use Htaccess to rewrite "ugly" URL's to shorter and prettier.In your code, you change your htaccess.
RewriteEngine On
RewriteRule ^(.*)$ products.php?pid=$1 [QSA,L]
Then change your URL's.
<?php
$i=1;
while($i <= 10){
echo '<a href="products.php/'.$i.'">Products '.$i.'</a><br />';
$i++;
}
?>