Hi ... how can i have url seo friendly from database ?? Example:
localhost/myshop/product.php? id = 95
Or:
https://www.myshop.it/product.php?id=95

I have done several tests with .htccess but it doesn't work.
I would like to have as follows:

https://www.myshop.it/product.php/shoes-armani
Or:
https://www.myshop.it/shoes-armani

tbl_product
+-------------+----------------+-----+----------------+
| p_id        |p_name                | p_name_friendly|
+-------------+----------------------+----------------+
|           95| Shoes Armani         |shoes-armani    |
|           96| Bag Cartier  |       |bag-cartier     |
+-------------+----------------------+----------------!

Recommended Answers

I have to ask why do this. I've been watching users for years and they don't care what the URL is if they are shopping.

Jump to Post

I have to ask why do this. I've been watching users for years and they don't care what the URL is if they are shopping.

This is considered one of the most important things to do for both SEO as well as usability reasons. I could go into …

Jump to Post

All 5 Replies

I have to ask why do this. I've been watching users for years and they don't care what the URL is if they are shopping.

I have to ask why do this. I've been watching users for years and they don't care what the URL is if they are shopping.

This is considered one of the most important things to do for both SEO as well as usability reasons. I could go into a whole laundry list explanation that would be paragraphs and paragraphs long of all the benefits, but I'll spare you the details.

Adolfo, this needs to be done in two steps if you wish to do a database lookup.

The simple way of doing it is to spare the database lookup, the way we do it here on DaniWeb. For example, you can see our URL Is:

https://www.daniweb.com/programming/threads/536532/how-obtain-url-friendly-with-htaccess

We include the friendly slug as well as the topic ID # to spare the overhead of a database lookup.

If you do want to do it with a database lookup, there's the tradeoff. You get the cleaner URLs but it's at the sacrifice of overhead.

To do it that way, you need to first use .htaccess (assuming you are using an Apache web server, you need to use nginx.conf if you are using an Nginx web server) to redirect:

https://www.myshop.it/shoes-armani to https://www.myshop.it/product.php?slug=shoes-armani

(Let me know if you need further help with this part, and show me the .htaccess file you currently have)

Then, the product.php file should do a database lookup based on the value of the slug passed in to get the p_id, and then show the product information based on that p_id.

So, just to recap, instead of product.php?id=95 you would do product.php?slug=shoes-armani

Then, instead of a database lookup for p_id=95, you would do a database lookup for p_name_friendly=shoes-armani

You would also set .htaccess to redirect myshop.it/shoes-armani to myshop.it/product.php?slug=shoes-armani

Good luck, and let me know if you need more assistance!!

commented: Thanks for pointing out a SEO angle. +16
commented: it's however extremely fragile as such links can easily change over time and now you've stale information in your permalinks... +16

If you are using a shopping cart you downloaded, such as OpenCart, there are built in options that allow you to do this.

commented: Yes, I know, but this is another project that I am trying to improve +1

Thanks Dani ...
I solved it, with some problem in Update Cart, and as you said, changing url in category page:

<a href="product.php?id=<?php echo $row['p_id']; ?></a>

result = product.php?id=93
In:

<a href="product.php?id=<?php echo $row['p_name_friendly']; ?></a>

result = product.php?id=shoes-armani

It is possible with htaccess to obtain (??):
result = product.php/shoes-armani
My htaccess test not working

RewriteEngine On
##RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1 [NC]

##RewriteRule ^([-\w]+)$ product.php?url=$1 [NC,L]


##RewriteRule ^/product/?([^/]*)/ \/product.php?id=$1 [PT]
##RewriteRule ^product/(\w+)/?$ product.php?id=$1

##RewriteRule ^([^/\.]+)/?$ product.php?id=$1
RewriteRule  ^product/(.*)$/?$      product.php?p_name_friendly=$1      [NC,L]

If your links look like <a href="product.php?id=<?php echo $row['p_name_friendly']; ?>">Click Here</a> then that's what users are going to browse to (the URL with the query string parameters), and there's no advantage to doing URL rewriting. You want your HTML links to look like <a href="product/<?php echo $row['p_name_friendly']; ?>">Click Here</a>

Then, you want users to browse to /product/foo and have it internally process product.php, as so:

RewriteEngine On
RewriteRule  ^product/(.+)$      product.php?p_name_friendly=$1      [NC,L]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.