I have been thinking for quite some time now about how I could manage thousands of webpages that change based on a set of data in MySQL.
So in essence, I have thousands of "products" and each is represented by a row in a database table.
What I do currently to page each of these "products" viewable via a url is place a .php file in the url location so that when a user visits http://www.mydomain.com/some_random_product.php
they are opening one of many .php files that all contain the following:
<?php
$thisPageName = basename(__FILE__, ".php");
include '/out/of/root/allProducts.controller.php';
?>
So essentially all is hand off the url name to another script that takes the url name then searches the database for a corresponding "product", if it finds the product then it displays a product page with the relevant information and if not it displays an error page stating that the product could not be found.
What I would like to do now, is better that process so that I would not need to create thousands of physical files on the server. Even though this is quite a simple task as I have a script to do it, it just becomes a pain having to run the thing all the time (I have to do it locally as it is a resource hog, then copy all the pages across to the server).
Possibly just one file that all of the urls in that location would load without changing the url as I need it to find the relating info.
I would like to add that the server running the domain is a VPS that I have full access to and I understand that the Apache mod_rewrite module may be of use here but I honestly would not know where to even start in terms of the logic behind implementing that.
If anyone could suggest a better way to do this or anything that may be of relevance then it would be greatly appreciated, thank you!
Pass the product ID in the URL and catch it in your PHP script with $_GET[productId]
.
Then you should query the database to get the row of the product matching that ID and display the data of that product.
To make the URL's pretty for this case and stay SEO friendly:
#.htaccess
RewriteEngine On
RewriteRule ^product/([0-9]+)/(.*)$ /product/product.php?productId=$1&name=$2
# URL:
<a href="/product/product.php?productId=123&name=product-one">
Product one
</a>
<!-- OR (Because of the URL rewriting) -->
<a href="/product/123/product-one">
Product one
</a>
# PHP Script
$id = $_GET[productId];
$sql = "SELECT *
FROM `product`
WHERE `id` = ?;";
etc...
This way you could show thousands of different content on just one simple PHP page.
It looks like you have several questions in one here.
Firstly you need to build a dynamic page that retrieves the product information from the database and displays this, based on a unique identifier passed to the page through a querystring/GET parameter.
Example of GET parameter usage in PHP from php.net:
<?php
echo 'Hello ' . $_GET["name"] . '!';
?>
Assuming the user entered http://example.com/?name=Hannes
The above example will output something similar to: Hello Hannes!
Secondly you want to use mod_rewrite to allow pretty URLs to be accessed, i.e: example.com/products/some_random_product
vs example.com/products.php?id=some_random_product
.
Example Apache mod_rewrite condition to achieve this:
RewriteEngine on
RewriteRule ^products/([^/.]+)/?$ products.php?id=$1 [L]
Based on the questions you've asked I would recommend you do some research into mod_rewrite
, $_GET
, and in general the use of PHP to create dynamic web pages.
Edit on SEO:
Providing you use mod_rewrite there should be no difference in SEO. The client won't be able to tell that you are really displaying products.php
rather than /products/some_random_product
. mod_rewrite is entirely server-side, it is completely different to sending a response to the client telling them the page has moved for example.