Pretty Urls (htaccess file?)

Hi,

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’]; ?>" >

Currently:

/products/product/123456

Change to:

/products/product/red-widget

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?

Hi,

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.

johnlewis.com/john-lewis-lasko-chest-of-drawers-oak/p230674902

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

johnlewis.com/john-lewis-lasko-chest-of-drawers-oak

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?

Hi,

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 johnlewis.com/p230674902-john-lewis-lasko-chest-of-drawers-oak 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.

yoursite.com/products/123456/large-red-widget

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:

yoursite.com/products/123457/large-red-widget

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']; ?>

<?php
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
?>

Thanks,

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.

Hi,

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
</IfModule>

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

Hi,

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:

Hi,

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.

Hi,

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.