Reputation: 1065
What I want to do:
Create dynamic php file for each product. What I'm currently doing is, if a person clicks on a products , it opens in one single file with some data i.e. www.example.com/showproduct.php?ref=1085
. Here 1085 is a productid & thus it shows the content from DB for the particular id.
Is it possible to show the URL as www.example.com/google-nexus-5-16gb-black.php
, where this data comes from DB content?
I don't want to create a page for each product, I think that would be a waste. Is there any solution? You can see even on stackoverflow
how it shows the URL for each question.
EDIT
I've used following code in .htaccess
file, however it's not rewriting the URL.
RewriteEngine on
RewriteRule ^ref/([A-Za-z0-9-]+)/?$ showproduct.php?ref=$1 [NC]
Any problem with this? And my I will try connecting to DB & get the file name once I know how this works.
Upvotes: 0
Views: 108
Reputation: 3577
You can create a unique alias field for each product record in your DB, and then you can use mod_rewrite
to map www.example.com/YOUR-ALIAS
to www.example.com/showproduct.php?alias=YOUR-ALIAS
. This will give your the desired friendly URL effect.
In your case it appears that you merely want to map google-nexus-5-16gb-black.php
to showproduct.php?ref=1085
, creating a additional alias field may be overkilled. In this case, you may setup a single rewrite rule:
RewriteEngine on
RewriteRule ^google-nexus-5-16gb-black.php$ /showproduct.php?ref=1085 [L]
Upvotes: 2
Reputation: 14064
URL rewriting file. Redirecting original URL www.example.com/showproduct.php?ref=1085 to www.example.com/google-nexus-5-16gb-black.php
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+).php$ showproduct.php?ref=$1
RewriteRule ^([a-zA-Z0-9-/]+).php/$ showproduct.php?ref=$1
You can read more about Basic URL Rewrite
<?php
include('db.php'); // Connection to your database
if($_GET['ref'])
{
$url=mysql_real_escape_string($_GET['ref']);
$sql=mysql_query("select title from table where ref='$url'");
$count=mysql_num_rows($sql);
$row=mysql_fetch_array($sql);
$title=$row['title'];
}
?>
Probably this link will help you to do what you are looking for
Upvotes: 0