Pretty Urls (htaccess file?)


I am querying my database with a unique product code. However I want the URL to dispaly the $linkname does anyone know how I do this please.

For example: /products/product/<?php echo $row[‘product_id’]; ?>" >



Change to:


I have added a link name to database called $linkname. For example “large-red-widget”. However my link is querying the database using a unique product ID but I want to display the $linkname within link.

What I dont understand is the relationship between the htaccess file and the way the link is displayed. How I do I display link with linkname but query the databsae using the product_id ?

This works…

/products/product/<?php echo $row[‘product_id’]; ?>" >

RewriteRule ^products/product/([a-zA-Z0-9]+)$ /products/product.php?product_id=$1

This doesn’t…

/products/product/<?php echo $row[‘product_id’]; ?>" >

RewriteRule ^products/product/([a-zA-Z0-9]+)$ /products/product.php?linkname=$1

Firstly you’d have to change the code in product.php to query your db on the linkname column, rather than the product_id. You’d also need to change the code that generates your links, so that they include the name rather than the id, similar to this:

/products/product/<?php echo $row['linkname']; ?>

Are you using some kind of ecommerce software or anything like that?


No its a site a built myself using PHP.

The thing is I want to run the query using a unique code which I am currently doing. How do I query the database using one variable but display a different variable in the link.

This is a classic example of what I am trying to do.

But is it possible to do this without the product ID. So its just but still displays product “p230674902”

What table structure do you have? Is all the product info in one table, and then you have a second table that maps the linkname to the product_id?


No these two variables are in the same table.

So instead of the existing query, which must be something along these lines:

SELECT * FROM products WHERE product_id = $product_id

why can’t you just do this?

SELECT * FROM products WHERE linkname = $linkname

Because the link name is not a unique identifier.

I thought I would need a unique identifier?

Right, yes you would… sorry, I’d assumed that your link name was unique. In the case that it’s not, what you have to do is include the product ID in the url, something like to use your example. I expect this brings you back to square one, but unfortunately if the link name is not unique then there is no way around it.

Your “link name” is commonly referred to as a slug. You need a unique identifier somewhere in your URL to retrieve the appropriate page. If you have redundant slugs, you will have to have a product ID or some other unique identifier in your URL.

123456 is the product ID in this case which would be used to query the table. The large-red-widget is a dummy slug in this case for the benefit of people or search engines reading the URL and is not used in the code. So you could do something like this:

Same dummy slug but different product ID.

Its very strange because when I try to run the query with the linkname and change the GET on the following page the following page does not display anything.

/products/product/<?php echo $row['linkname']; ?>

if (isset($_GET['linkname']))
$linkname = mysql_real_escape_string($_GET['linkname']);
$sql = "SELECT * FROM productdbase WHERE linkname = '$linkname'";
$res = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($res); // no need to loop since you are retrieving only one row
$num_rows = mysql_num_rows($res); // check to see if any results were found, just in case someone puts an ID in the url without clicking on your link


This looks good. How do I add the ‘dummy slug’ into the link whilst still querying the unique ID.

/products/<?php echo $row['product_id'] . '/' . $row['linkname']; ?>

Something like that. I didn’t test. Also, do you have a bug in your if syntax from earlier where you forgot the brackets?

You also need to change your rewrite rule a little:

RewriteRule ^products/product/([a-zA-Z0-9]+)/(.*)$ /products/product.php?product_id=$1

This should let you have URLs in the same format as in cheesedude’s example.


I tried that but it failed. Should I have htaccess file like this and link as per below?

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^ index.php [L]
 RewriteRule ^products/product/([a-zA-Z0-9]+)/(.*)$ /products/product.php?product_id=$1

/products/product/<?php echo $row['product_id']; ?>/<?php echo $row['linkname']; ?>" >


I tried this but it doesn’t display anything.

<?php echo $row['product_id'] . '/' . $row['linkname']; ?>

I think you might need to swap the order of those last two rules, and add a couple of flags like this:

RewriteRule ^products/product/([a-zA-Z0-9]+)/(.*)$ /products/product.php?product_id=$1 [QSA, L]
RewriteRule ^ index.php [L]

but I’m not an expert in mod_rewrite rules, so you might want to run it past someone over in this forum:


Ive tried Googling “slugs” and “pretty urls” but nothing is coming up.

Is there a best basic guide on how to use slugs in pretty urls?

Try calling var_dump($row) to check if you’re actually getting a record back from the DB.


I tried that and it does echo. I can echo it into the link and treat it as a normal variable.

Im stuck on using it as a slug within the link.